iOS中有时候需要加载一些Gif动画图片,从实现方式和性能上考虑目前FLAnimatedImage比较适合.

基础实现

安装FLAnimatedImage第三方库,导入头文件FLAnimatedImage,加载Gif图片:

    self.showBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];self.showBgView.center = self.view.center;NSURL *imgUrl = [[NSBundle mainBundle] URLForResource:@"FlyElephant" withExtension:@"gif"];FLAnimatedImage *animatedImg = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:imgUrl]];self.animatedImgView = [[FLAnimatedImageView alloc] init];self.animatedImgView.animatedImage = animatedImg;self.animatedImgView.frame = CGRectMake(0,0,100,100);[self.showBgView addSubview:self.animatedImgView];[self.view addSubview:self.showBgView];

循环次数

实现之后发现Gif图片无限播放,如果想只播放一次,然后删除动画,有两种方式,一种是根据Gif的时间,定时删除,第二种通过FLAnimatedImageView提供的回调实现.

@interface FLAnimatedImageView : UIImageView// Setting `[UIImageView.image]` to a non-`nil` value clears out existing `animatedImage`.
// And vice versa, setting `animatedImage` will initially populate the `[UIImageView.image]` to its `posterImage` and then start animating and hold `currentFrame`.
@property (nonatomic, strong) FLAnimatedImage *animatedImage;
@property (nonatomic, copy) void(^loopCompletionBlock)(NSUInteger loopCountRemaining);@property (nonatomic, strong, readonly) UIImage *currentFrame;
@property (nonatomic, assign, readonly) NSUInteger currentFrameIndex;// The animation runloop mode. Enables playback during scrolling by allowing timer events (i.e. animation) with NSRunLoopCommonModes.
// To keep scrolling smooth on single-core devices such as iPhone 3GS/4 and iPod Touch 4th gen, the default run loop mode is NSDefaultRunLoopMode. Otherwise, the default is NSDefaultRunLoopMode.
@property (nonatomic, copy) NSString *runLoopMode;@end

动画播放完成之后删除动画的实现:

    __weak typeof (self) weakSelf = self;self.animatedImgView.loopCompletionBlock = ^(NSUInteger loopCountRemaining){[weakSelf.showBgView removeFromSuperview];};

转载链接:https://www.jianshu.com/p/9e0f594d704c

iOS-FLAnimatedImage加载Gif相关推荐

  1. iOS WebView 加载本地资源(图片,文件等)

    NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil]; NSURL *url ...

  2. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化 在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用户体 ...

  3. ios html异步加载图片,iOS 异步加载本地图片

    iOS 异步加载本地图片 问题 当某个界面使用系统API + (nullable UIImage *)imageNamed:(NSString *)name;加载了过多本地图片资源时,不可避免的会产生 ...

  4. iOS WebView加载url缓存问题

    在做青海展厅项目时,将本应放在触摸屏的h5放在iPad pro上,于是做了个APP,写了个webView来load url request.但是由于在调试阶段,做h5的童鞋经常修改,然后发布到服务器上 ...

  5. android菊花动画,Android实现仿iOS菊花加载圈动画效果

    常见的实现方式 切图,做旋转动画 自定义View,绘制效果 gif图 1.切图会增加体积,但相对简单,不过在换肤的场景下,会使用不同颜色,需要准备多张图,不够灵活. 2.由于自定义的好处,不同颜色只需 ...

  6. ios 旋转加载gif_iOS 中gif图的显示

    一.前言 iOS开发中,大部分时候我们显示一张静态图就可以了,但是有的时候为了UI表现更生动,我就有可能需要展示gif图来达到效果了. 网上找了一下,显示gif图的框架找到了两个. SDWebImag ...

  7. IOS UITableView 加载未知宽高图片的解决方案

    在开发中遇到了UITableView列表 UITableViewCell装载图片但不知Image的宽高 问题. 在解决该问题的时候,首先想到的是异步加载图片 采用第三方框架SDWebImage 实现对 ...

  8. ios 旋转加载gif_加载GIF动画方法 iOS

    方法一 使用UIWebView _codeStr为gif网址      如果是本地的gif可以直接使用dataWithContentsOfFile方法 NSData *data = [NSData d ...

  9. iOS打开沙盒html,iOS WKWebView加载本地/沙盒HTML及css、images

    注意这里加载的是本地的不是沙盒里的,加载沙盒的方法在最下面. 先上代码:NSString *path = [[NSBundle mainBundle] pathForResource:@"l ...

  10. iOS中加载Flutter中的图片

    在 Flutter 插件开发中,有时需要将 Flutter 中配置的图片资源传递到 Android 或者是 iOS原生中,传递方法如下: //一般应用在Flutter 插件开发中 //注册插件的方法 ...

最新文章

  1. Spring基础专题——第十一章(高级注解编程完结)
  2. 【 MATLAB 】用 MATLAB 实现离散时间傅里叶变换(DTFT)的两个案例分析
  3. 神策营销云:「在线教育」行业,如何借“运营工具”玩转微信生态?
  4. excel表格大学计算机知识,大学计算机基础excel电子表格
  5. javascript的数组之reduce()
  6. 【LiveVideoStack线上分享】FFmpeg深度学习模块架构与代码实践
  7. 无法访问此网站 localhost 拒绝了我们的连接请求。_网站经常被DDOS攻击会造成什么影响...
  8. 关于DLL中的哪段代码被执行的处理方法
  9. 一致性hash算法的应用研究学习
  10. mysql的初始化语句是_MySQL入门之预处理语句的使用
  11. DOSbox汇编集成环境下的详细设置
  12. python调用百度地图,通过经纬度定位
  13. 支付宝报错 未设置签名参数
  14. 数据库设计案例(1)
  15. Android新浪微博实训报告,完成一份新浪微博目标用户分析报告
  16. excel不能复制粘贴怎么回事
  17. Java核心(集合类1-概述、Collection 接口 、List 集合)
  18. ios快捷指令:修改图片尺寸、拼接长图
  19. 抖音直播间获取高清视频地址
  20. RUST语言的编程范式

热门文章

  1. 请问如何恢复桌面不见的IE图标?
  2. ctfshow单身杯部分wp
  3. Vim基础用法,最常用、最实用的命令介绍(保姆级教程)
  4. 微型计算机celeron是指,网络自主学习平台综合测试选择题答案
  5. java处理网络图片
  6. php-ml 逻辑回归,TensorFlow ML cookbook 第三章6-8节 套索和岭回归、弹性网络回归and Logistic回归...
  7. 美团点评西安站面经整理
  8. python 可执行文件大_为什么 python3.6 的动态链接库比 python2.7 的可执行文件大了很多?...
  9. Springboot 集成Redis 哨兵模式
  10. 京东年货节,如何一键生成全店手机详情?