UIImageView+MOSvga.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // UIImageView+MOSvga.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/11/23.
  6. //
  7. #import "UIImageView+MOSvga.h"
  8. #import "SVGA.h"
  9. static SVGAParser *parser;
  10. @implementation UIImageView (MOSvga)
  11. - (void)mo_setImageWithUrl:(NSString *)urlString placeholder:(UIImage *)placeholderImage loops:(int)loops AndFrame:(CGRect)rectFrame{
  12. //-135只是一个自定义的tag值,避免一些常用的1.2.3.4.5等值。
  13. SVGAPlayer *player = [self viewWithTag:-135];
  14. [player stopAnimation];
  15. //创建SVGAPlayer
  16. if (player == nil)
  17. {
  18. //跟imageView大小相同
  19. player = [[SVGAPlayer alloc] init];
  20. player.tag = -135;
  21. //超出边框的需要被裁剪,否则出现出界的情况
  22. self.clipsToBounds = YES;
  23. [self addSubview:player];
  24. }
  25. //每次重新给player的frame赋值,不然刷新时,部分会出现宽高为0的问题
  26. player.frame = rectFrame;
  27. //创建解析对象
  28. parser = [[SVGAParser alloc] init];
  29. //从url中解析
  30. [parser parseWithURL:[NSURL URLWithString:urlString] completionBlock:^(SVGAVideoEntity * _Nullable videoItem)
  31. {
  32. if (videoItem != nil)
  33. {
  34. player.hidden = NO;
  35. //tabBar的icon不置空
  36. //设置动画item
  37. player.videoItem = videoItem;
  38. //设置循环次数
  39. player.loops = loops;
  40. //停止后是否清除动画。默认YES,NO的话会停在最后一帧。
  41. player.clearsAfterStop = NO;
  42. //播放动画
  43. [player startAnimation];
  44. }
  45. } failureBlock:^(NSError * _Nonnull error)
  46. {
  47. MOLogV(@"SVGAParser error : %@",error);
  48. }];
  49. }
  50. @end