原文轉自:http://www.2cto.com/kf/201603/491897.html
- iOS-Gif圖片展示N種方式(原生+第三方)
-
原生方法:
1.UIWebView
特點:加載速度略長,性能更優,播放的gif動態圖更加流暢。12345678910111213<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
.h1234567891011<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
12345678910111213141516171819202122232425262728293031323334353637383940<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+Tool12345678<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