| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // UIImageView+MOSvga.m
- // MiMoLive
- //
- // Created by SuperC on 2023/11/23.
- //
- #import "UIImageView+MOSvga.h"
- #import "SVGA.h"
- static SVGAParser *parser;
- @implementation UIImageView (MOSvga)
- - (void)mo_setImageWithUrl:(NSString *)urlString placeholder:(UIImage *)placeholderImage loops:(int)loops AndFrame:(CGRect)rectFrame{
- //-135只是一个自定义的tag值,避免一些常用的1.2.3.4.5等值。
- SVGAPlayer *player = [self viewWithTag:-135];
- [player stopAnimation];
-
- //创建SVGAPlayer
- if (player == nil)
- {
- //跟imageView大小相同
- player = [[SVGAPlayer alloc] init];
- player.tag = -135;
- //超出边框的需要被裁剪,否则出现出界的情况
- self.clipsToBounds = YES;
- [self addSubview:player];
- }
-
- //每次重新给player的frame赋值,不然刷新时,部分会出现宽高为0的问题
- player.frame = rectFrame;
-
- //创建解析对象
- parser = [[SVGAParser alloc] init];
- //从url中解析
- [parser parseWithURL:[NSURL URLWithString:urlString] completionBlock:^(SVGAVideoEntity * _Nullable videoItem)
- {
- if (videoItem != nil)
- {
- player.hidden = NO;
- //tabBar的icon不置空
- //设置动画item
- player.videoItem = videoItem;
- //设置循环次数
- player.loops = loops;
- //停止后是否清除动画。默认YES,NO的话会停在最后一帧。
- player.clearsAfterStop = NO;
- //播放动画
- [player startAnimation];
- }
-
- } failureBlock:^(NSError * _Nonnull error)
- {
- MOLogV(@"SVGAParser error : %@",error);
- }];
- }
- @end
|