iOS-Gif图片展示N种方式(原生+第三方)


原文转自:http://www.2cto.com/kf/201603/491897.html



首页 >  程序开发 >  移动开发 >  IOS > 正文
iOS-Gif图片展示N种方式(原生+第三方)
2016-03-03       0  个评论    来源:iOS爱好者  
收藏     我要投稿

原生方法:

1.UIWebView
特点:加载速度略长,性能更优,播放的gif动态图更加流畅。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<code class = "hljs objectivec" > //动态展示GIF图片-WebView
-( void )showGifImageWithWebView{
     //读取gif图片数据
     NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@ "earthGif" ofType:@ "gif" ]];
     //UIWebView生成
     UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake( 112 , 302 , 132 , 102 )];
     //用户不可交互
     imageWebView.userInteractionEnabled = NO;
     //加载gif数据
     [imageWebView loadData:gifData MIMEType:@ "image/gif" textEncodingName:nil baseURL:nil];
     //视图添加此gif控件
     [self.view addSubview:imageWebView];
}</code>

2.UIImagView
加载的方式更加快速,性能不如UIWebView,优点:易于扩展

1)
增加一个UIImageView的类别(category),增加两个方法
UIImage+Tool
.h

?
1
2
3
4
5
6
7
8
9
10
11
<code class = "hljs java" ># import <uikit uikit.h= "" >
 
@interface UIImageView (Tool)
 
/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */
-( void )getGifImageWithUrk:(NSURL *)url returnData:( void (^)(NSArray<uiimage> * imageArray,NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths, NSArray<nsnumber>* heights))dataBlock;
 
/** 为UIImageView添加一个设置gif图内容的方法: */
-( void )yh_setImage:(NSURL *)imageUrl;
 
@end </nsnumber></nsnumber></nsnumber></uiimage></uikit></code>

.m

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<code class = "hljs objectivec" > //
//  UIImageView+Tool.m
//  OneHelper
//
//  Created by qiuxuewei on 16/3/2.
//  Copyright ? 2016年 邱学伟. All rights reserved.
//
 
# import "UIImageView+Tool.h"
//要引入ImageIO库
# import <imageio imageio.h= "" >
 
@implementation UIImageView (Tool)
 
 
 
//解析gif文件数据的方法 block中会将解析的数据传递出来
-( void )getGifImageWithUrk:(NSURL *)url returnData:( void (^)(NSArray<uiimage> * imageArray, NSArray<nsnumber>*timeArray,CGFloat totalTime, NSArray<nsnumber>* widths,NSArray<nsnumber>* heights))dataBlock{
     //通过文件的url来将gif文件读取为图片数据引用
     CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
     //获取gif文件中图片的个数
     size_t count = CGImageSourceGetCount(source);
     //定义一个变量记录gif播放一轮的时间
     float allTime= 0 ;
     //存放所有图片
     NSMutableArray * imageArray = [[NSMutableArray alloc]init];
     //存放每一帧播放的时间
     NSMutableArray * timeArray = [[NSMutableArray alloc]init];
     //存放每张图片的宽度 (一般在一个gif文件中,所有图片尺寸都会一样)
     NSMutableArray * widthArray = [[NSMutableArray alloc]init];
     //存放每张图片的高度
     NSMutableArray * heightArray = [[NSMutableArray alloc]init];
     //遍历
     for (size_t i= 0 ; i<count; __bridge= "" __self= "self;" __weak= "" cgfloat= "" cgimageref= "" getgifimagewithurk:imageurl= "" height= "[[info" heightarray= "" id= "" image= "CGImageSourceCreateImageAtIndex(source," imagearray= "" info= "(__bridge" nsdictionary= "" nsnumber= "" nsstring= "" nsurl= "" self= "" time= "[[timeDic" timearray= "" timedic= "[info" uiimage= "" width= "[[info" widtharray= "" > *imageArray, NSArray<nsnumber> *timeArray, CGFloat totalTime, NSArray<nsnumber> *widths, NSArray<nsnumber> *heights) {
         //添加帧动画
         CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@ "contents" ];
         NSMutableArray * times = [[NSMutableArray alloc]init];
         float currentTime = 0 ;
         //设置每一帧的时间占比
         for ( int i= 0 ; i<imagearray.count; __self= "" addanimation:animation= "" animation= "" animation.duration= "totalTime;" animation.repeatcount= "MAXFLOAT;" camediatimingfunction= "" code= "" end= "" nsnumber= "" numberwithfloat:currenttime= "" times= "" uiimageview= "" ></imagearray.count;></nsnumber></nsnumber></nsnumber></count;></nsnumber></nsnumber></nsnumber></uiimage></imageio></code>

在加载gif的地方使用
导入 UIImageView+Tool

?
1
2
3
4
5
6
7
8
<code class = "hljs objectivec" ><code class = "hljs objectivec" >-( void )showGifImageWithImageView{
 
     UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake( 112 , 342 , 132 , 102 )];
     NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@ "earthGif.gif" ofType:nil]];
     [imageView yh_setImage:url];
     [self.view addSubview:imageView];
 
}</code></code>

第三方:
1.YLGIFImage
github链接: https://github.com/liyong03/YLGIFImage


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值