iOS加载Gif图片的N种方式
2016-07-15 16:26
579 查看
1.系统UIImageView 多张图片组成动画
2.利用第三方库
1)IImageView-PlayGIF YFGIFImageView
2)SCGIFImageView
3)YLGIFImage
4)SDWebImageView里的UIImage+GIF
提供接口:
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
为MBProgressHUD 添加加载动画
其它
微博客户端 VVebo 的作者开源了他自己为VVebo写的GIF解决方案 VVeboImageView, 占用内存很小。(iOS移动开发周报-第4期)
Demo下载
http://yunpan.cn/cj6JBN7mfETWE (提取码:226f)
/** * 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期)
Demo下载
http://yunpan.cn/cj6JBN7mfETWE (提取码:226f)
相关文章推荐
- 视图设置问题造成的CALayerInvalidGeometry错误
- iOS-控件响应用户控制事件之事件处理
- iOS 使用Quartz和OpenGL绘图
- IOS开发微博中涉及到的一些正则表达式(表情,话题,链接,@)
- 史上最全的iOS各种设备信息获取总结
- 深入了解iOS中的VC切换的传值方式
- 清除缓存
- ios developer tiny share-20160715
- ios developer tiny share-20160715
- ios 感觉苹果少了个方法,求啪啪啪打脸
- iOS开发的小技巧
- socket
- iOS 取出当前日期 并格式化
- iOS:基于AVPlayer实现的视频播放器
- 2016最新FFmpeg-iOS静态库编译
- 从Immutable来谈谈对于线程安全的理解误区
- 李洪强iOS开发之XMPP
- 最新的 iOS 申请证书与发布流程(2016.12)
- iOS arm64 armv7指令集
- iOS9全面拥抱64位 iPhone4S/5/5C悲剧了