Gif圖片是非常常見的圖片格式锈嫩,尤其是在聊天的過程中,Gif表情使用地很頻繁甥角。但是iOS竟然沒有現(xiàn)成的支持加載和播放Gif的類网严。
簡(jiǎn)單地匯總了一下,大概有以下幾種方法:
一蜈膨、加載本地Gif文件
1屿笼、使用UIWebView
// 讀取gif圖片數(shù)據(jù) 注意:傳入nil參數(shù)可能有警告NSData *data = [NSDatadataWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"001"ofType:@"gif"]];? ? ? ? UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,200,200)];? ? [webViewloadData:dataMIMEType:@"image/gif"textEncodingName:nilbaseURL:nil];? ? [self.viewaddSubview:webView];
但是使用UIWebView的弊端在于,不能設(shè)置Gif動(dòng)畫的播放時(shí)間翁巍。
2、將Gif拆分成多張圖片休雌,使用UIImageView播放
最好把所需要的Gif圖片打包到Bundle文件內(nèi),如下圖所示
Loading.png
- (NSArray*)animationImages{NSFileManager*fielM = [NSFileManagerdefaultManager];NSString*path = [[NSBundlemainBundle] pathForResource:@"Loading"ofType:@"bundle"];NSArray*arrays = [fielM contentsOfDirectoryAtPath:path error:nil];NSMutableArray*imagesArr = [NSMutableArrayarray];for(NSString*nameinarrays) {UIImage*image = [UIImageimageNamed:[(@"Loading.bundle") stringByAppendingPathComponent:name]];if(image) {? ? ? ? ? ? [imagesArr addObject:image];? ? ? ? }? ? }returnimagesArr;}- (void)viewDidLoad {? ? [superviewDidLoad];UIImageView*gifImageView = [[UIImageViewalloc] initWithFrame:frame];? ? gifImageView.animationImages = [selfanimationImages];//獲取Gif圖片列表gifImageView.animationDuration =5;//執(zhí)行一次完整動(dòng)畫所需的時(shí)長(zhǎng)gifImageView.animationRepeatCount =1;//動(dòng)畫重復(fù)次數(shù)[gifImageView startAnimating];? ? [self.view addSubview:gifImageView];}
3灶壶、使用SDWebImage
但是很遺憾,SDWebImage的sd_setImageWithURL:placeholderImage:這個(gè)方法是不能播放本地Gif的杈曲,它只能顯示Gif的第一張圖片而已驰凛。So,此方法行不通
UIImageView *gifImageView = [[UIImageView alloc]initWithFrame:frame];? ? [gifImageViewsd_setImageWithURL:nilplaceholderImage:[UIImageimageNamed:@"gifTest.gif"]];
其實(shí)担扑,在SDWebImage這個(gè)庫里有一個(gè)UIImage+GIF的類別恰响,里面為UIImage擴(kuò)展了三個(gè)方法:
@interfaceUIImage(GIF)+ (IImage *)sd_animatedGIFNamed:(NSString*)name;+ (UIImage*)sd_animatedGIFWithData:(NSData*)data;- (UIImage*)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;@end
大家一看就知道,我們要獲取處理后的Gif圖片涌献,其實(shí)只要調(diào)用前面兩個(gè)中的其中一個(gè)方法就行了
注意:第一個(gè)只需要傳Gif的名字胚宦,而不需要帶擴(kuò)展名(如Gif圖片名字為001@2x.gif,只需傳001即可)
我們就使用第二個(gè)方法試一試效果:
NSString*path = [[NSBundlemainBundle] pathForResource:@"gifTest"ofType:@"gif"];NSData*data = [NSDatadataWithContentsOfFile:path];UIImage*image = [UIImagesd_animatedGIFWithData:data];? ? gifImageView.image = image;
然后通過斷點(diǎn),我們看下獲取到的image是個(gè)什么樣的東東:
img.png
我們發(fā)現(xiàn):
image的isa指針指向了_UIAnimatedImage枢劝,說明它是一個(gè)叫作_UIAnimatedImage的類(當(dāng)然井联,這個(gè)_UIAnimatedImage蘋果是不會(huì)直接讓我們使用的)
_images表示:這個(gè)Gif包含了多少?gòu)垐D片
_duration表示:執(zhí)行一次完整動(dòng)畫所需的時(shí)長(zhǎng)
其實(shí),動(dòng)畫執(zhí)續(xù)時(shí)間_duration也可以更改您旁!
我們來看下此方法的內(nèi)部實(shí)現(xiàn):
+ (UIImage*)sd_animatedGIFWithData:(NSData*)data {if(!data) {returnnil;? ? }CGImageSourceRefsource =CGImageSourceCreateWithData((__bridgeCFDataRef)data,NULL);? ? size_t count =CGImageSourceGetCount(source);UIImage*animatedImage;if(count <=1) {? ? ? ? animatedImage = [[UIImagealloc] initWithData:data];? ? }else{NSMutableArray*images = [NSMutableArrayarray];NSTimeIntervalduration =0.0f;for(size_t i =0; i < count; i++) {CGImageRefimage =CGImageSourceCreateImageAtIndex(source, i,NULL);? ? ? ? ? ? duration += [selfsd_frameDurationAtIndex:i source:source];? ? ? ? ? ? [images addObject:[UIImageimageWithCGImage:image scale:[UIScreenmainScreen].scale orientation:UIImageOrientationUp]];CGImageRelease(image);? ? ? ? }if(!duration) {? ? ? ? ? ? duration = (1.0f /10.0f) * count;? ? ? ? }? ? ? ? animatedImage = [UIImageanimatedImageWithImages:images duration:duration];? ? }CFRelease(source);returnanimatedImage;}
很明顯烙常,duration是可以隨意更改的,只不過此方法設(shè)置了一個(gè)默認(rèn)值
(duration = (1.0f / 10.0f) * count)
歸根到底鹤盒,創(chuàng)建新的動(dòng)態(tài)的Image其實(shí)是調(diào)用了系統(tǒng)提供的一個(gè)UIImage的類方法而已:
UIImage *animatedImage = [UIImageanimatedImageWithImages:imagesduration:duration];
二蚕脏、加載網(wǎng)絡(luò)Gif文件
加載網(wǎng)絡(luò)的Gif文件就簡(jiǎn)單多了。最簡(jiǎn)單的方法侦锯,我們只需要使用SDWebImage的sd_setImageWithURL:這個(gè)方法傳入Gif文件是url地址即可驼鞭。
糾其原因:稍微仔細(xì)看了SDWebImage內(nèi)部實(shí)現(xiàn)就可以清楚,大概是以下幾個(gè)步驟:
1率触、SDWebImage根據(jù)url將Gif文件下載下來终议,格式為一個(gè)NSData
2、如果判斷是Gif格式葱蝗,則會(huì)調(diào)用sd_animatedGIFWithData:將Data轉(zhuǎn)換成我們需要的Gif格式
3穴张、通過上面的方法二即可顯示出Gif圖片
UIImage *image= [UIImage sd_animatedGIFWithData:data];gifImageView.image=image;
............................................................
總結(jié)
一、加載本地Gif文件
1两曼、使用UIWebView不可以設(shè)置duration皂甘,其他兩種方法都可設(shè)置。而且方法1的容器為UIWebView悼凑,其余兩種的容器都是大家熟悉的UIImageView
2偿枕、方法2和方法3需要對(duì)應(yīng)看應(yīng)用場(chǎng)景
如:下拉、上拉加載控件需要一個(gè)根據(jù)拉動(dòng)距離設(shè)置特定的Image户辫,則需要使用方法2
直接顯示Gif圖片渐夸,則使用方法3會(huì)更方便
二、加載網(wǎng)絡(luò)Gif文件
直接使用SDWebImage的sd_setImageWithURL:這個(gè)方法傳入Gif文件是url地址即可