iOS加载Gif图片的N种方式 By-H罗
1.系统UIImageView 多张图片组成动画
/**
* UIImageView 动画
* Memory-23M
*/
-(void)gifPlay1
{
// NSArray *array=@[@"image0.png",@"image1.png",@"image2.png"];
// UIImageView *imgview= [UIImageView imageViewAnimation:CGRectMake(50,80, 550/2, 200) imageNames:array duration:1]; UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,80, 550/2, 200)];
animatedImageView.animationImages =@[[UIImage imageNamed:@"image0"],
[UIImage imageNamed:@"image1"],
[UIImage imageNamed:@"image2"],
];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[self.view addSubview: animatedImageView];
[animatedImageView startAnimating];
}
2.利用第三方库
1)IImageView-PlayGIF YFGIFImageView
/**
* UIImageView-PlayGIF 是 UIImageView 子类,用来显示 GIF。UIIMageView-PlayGIF 性能高,而且占用的内存很低。
* https://github.com/yfme/UIImageView-PlayGIF
* Memory-21.9M
* #import "YFGIFImageView.h"
*/
-(void)gifPlay2
{
NSString *gifPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"gif"];
YFGIFImageView *gifview=[[YFGIFImageView alloc]init];
gifview.backgroundColor=[UIColor clearColor];
gifview.gifPath=gifPath;
gifview.frame=CGRectMake(50, 100,550/2, 200);
[self.view addSubview:gifview];
[gifview startGIF];
}
2)SCGIFImageView
/**
* 摘要: SCGIFImageView是一个开源的GIF图片动画显示控件,通过将GIF的每一帧都取出来生成UIImage对象存放在一个数组中,然后使用NSTimer进行动画轮转。
* https://github.com/shichangone/SCGifExample
* Memory-22.5M
* #import "SCGIFImageView.h"
*/
-(void)gifPlay3
{
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"test.gif" ofType:nil];
NSData* imageData = [NSData dataWithContentsOfFile:filePath];
SCGIFImageView* gifImageView = [[SCGIFImageView alloc]init];
[gifImageView setData:imageData];
gifImageView.frame = CGRectMake(50,100, gifImageView.image.size.width, gifImageView.image.size.height);
[self.view addSubview:gifImageView];
}
3)YLGIFImage
/**
* YLGIFImage 是异步 GIF 图像解码器和图像查看器,支持播放 GIF 图像,而且使用很少的内存。
* https://github.com/liyong03/YLGIFImage
* Memory-22.7M
* #import "YLImageView.h"
* #import "YLGIFImage.h"
*/
-(void)gifPlay5
{
YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(0, 160, 320, 240)];
[self.view addSubview:imageView];
imageView.image = [YLGIFImage imageNamed:@"test.gif"];
}
4)SDWebImageView里的UIImage+GIF
提供接口:
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
/**
* 利用SDWebImageView 库播放gif
* Memory-22.6M
* #import "UIImage+GIF.h"
*/
-(void)gifPlay6
{
UIImage *image=[UIImage sd_animatedGIFNamed:@"test"];
UIImageView *gifview=[[UIImageView alloc]initWithFrame:CGRectMake(50,80,image.size.width, image.size.height)];
gifview.backgroundColor=[UIColor orangeColor];
gifview.image=image;
[self.view addSubview:gifview];
}
为MBProgressHUD 添加加载动画
/**
* MBProgressHUD 添加加载动画
* Memory-23.8M
* #import "UIImage+GIF.h"
* #import "MBProgressHUD.h"
*/
-(void)gifPlay6
{
UIImage *image=[UIImage sd_animatedGIFNamed:@"test"];
UIImageView *gifview=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,image.size.width/2, image.size.height/2)];
gifview.image=image; MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.color=[UIColor grayColor];//默认颜色太深了
hud.mode = MBProgressHUDModeCustomView;
hud.labelText = @"加载中...";
hud.customView=gifview;
}
其它
微博客户端 VVebo 的作者开源了他自己为VVebo写的GIF解决方案 VVeboImageView, 占用内存很小。(iOS移动开发周报-第4期)

iOS加载Gif图片的N种方式 By-H罗的更多相关文章
- qt加载背景图片的一种方式
//加载背景图片 void LCTGrid::loadBgPicture() { QImage image; QPalette palette; image.load(m_sPicturePath); ...
- UIImage加载本地图片的两种方式
UIImage加载图片方式一般有两种: (1)imagedNamed初始化:默认加载图片成功后会内存中缓存图片,这个方法用一个指定的名字在系统缓存中查找并返回一个图片对象.如果缓存中没有找到相应的图片 ...
- Unity加载本地图片的2种方式
1. 使用 WWW 加载,详细查看 unity3d 官方文档. 2. 使用 System.IO 加载,lua 代码如下: local File = luanet.import_type("S ...
- ios网络学习------4 UIWebView的加载本地数据的三种方式
ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...
- 加载xib文件的两种方式
一.加载xib文件的两种方式 1.方法一(NewsCell是xib文件的名称) NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@&quo ...
- 转 Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式 velocitypropertiespath Velocity中加载vm文件的三种方式: 方式一:加载classpath目录下的vm文件 Prope ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- Hadoop生态圈-注册并加载协处理器(coprocessor)的三种方式
Hadoop生态圈-注册并加载协处理器(coprocessor)的三种方式 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 到目前为止,大家已经掌握了如何使用过滤器来减少服务器端通过 ...
- Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式: a. 加载classpath目录下的vm文件 /** * 初始化Velocity引擎 * --VelocityEngine是单例模式,线程安全 * @th ...
随机推荐
- SQL Server 添加字段,修改字段类型,修改字段名,删除字段
-- 1.添加字段-- 基本语法alter table 表 add 列名 字段类型 null-- 例:给学生表添加Telephone字段并指定类型为vachar,长度为50,可空alter table ...
- SpringCloud创建Eureka模块
1.说明 本文详细介绍Spring Cloud创建Eureka模块的方法, 基于已经创建好的Spring Cloud父工程, 请参考SpringCloud创建项目父工程, 在里面创建Eureka模块, ...
- Sentry 企业级数据安全解决方案 - Relay 配置选项
Relay 的配置记录在文件 .relay/config.yml 中.要更改此位置,请将 --config 选项传递给任何 Relay 命令: ❯ ./relay run --config /path ...
- pytest用例的执行顺序
Pytest执行的顺序 当pytest运行测试函数时,它会查看该测试函数中的参数,然后搜索与这些参数具有相同名称的fixture.一旦pytest找到这些对象,它就会运行这些fixture 影响执行顺 ...
- MongoDB_安装、配置、连接(五)
MongoDB 是跨平台的,既可以在 Linux系统下安装,也可以在Windows 系统.MacOS系统下安装,本节主要介绍如何在 Linux 系统下安装 MongoDB. windows安装:htt ...
- [ unittest ] 使用初体验
import unittest from cal import Calculate class Mytest(unittest.TestCase): def setUp(self): self.cal ...
- Servlet部署描述符
注:图片如果损坏,点击文章链接:https://www.toutiao.com/i6512237744641540612/ <Servlet简单实现开发部署过程>中的过程,可以概括为以下模 ...
- 《Go组件设计与实现》-netpoll的总结
主要针对字节跳动的netpoll网络库进行总结.netpoll网络库相比于go本身的net标准库更适合高并发场景. 基础知识 netpoll与go.net库一样使用epoll这种IO多路复用机制处理网 ...
- idea同时启动多个微服务模块进行管理
1,打开IDEA项目中的 .idea 下 的workspace.xml 找到文件中的 RunDashboard 配置块,增加如下圈起来的地方 代码: <option name="con ...
- 计算机视觉2-> 深度学习 | anaconda+cuda+pytorch环境配置
00 想说的 深度学习的环境我配置了两个阶段,暑假的时候在一个主攻视觉的实验室干活,闲暇时候就顺手想给自己的Ubuntu1804配置一个深度学习的环境.这会儿配到了anaconda+pytorch+c ...