iOS加載Gif圖片

Gif圖片是非常常見(jiàn)的圖片格式,尤其是在聊天的過(guò)程中,Gif表情使用地很頻繁。但是iOS竟然沒(méi)有現(xiàn)成的支持加載和播放Gif的類(lèi)。

簡(jiǎn)單地匯總了一下,大概有以下幾種方法:

1、使用UIWebView,但是這種方法比較耗內(nèi)存。

//讀取gif圖片數(shù)據(jù)UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)];

[self.view addSubview:webView];

NSString*path = [[NSBundle mainBundle] pathForResource:@"001"ofType:@"gif"];/*NSData *data = [NSData dataWithContentsOfFile:path];

使用loadData:MIMEType:textEncodingName: 則有警告

[webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];*/NSURL*url =[NSURL URLWithString:path];

[webView loadRequest:[NSURLRequest requestWithURL:url]];

但是使用UIWebView的弊端在于,不能設(shè)置Gif動(dòng)畫(huà)的播放時(shí)間。

2、將Gif拆分成多張圖片,使用UIImageView播放。

最好把所需要的Gif圖片打包到Bundle文件內(nèi),如下圖所示

Loading.png

- (NSArray *)animationImages

{

NSFileManager*fielM =[NSFileManager defaultManager];

NSString*path = [[NSBundle mainBundle] pathForResource:@"Loading"ofType:@"bundle"];

NSArray*arrays =[fielM contentsOfDirectoryAtPath:path error:nil];

NSMutableArray*imagesArr =[NSMutableArray array];for(NSString *nameinarrays) {

UIImage*image = [UIImage imageNamed:[(@"Loading.bundle") stringByAppendingPathComponent:name]];if(image) {

[imagesArr addObject:image];

}

}returnimagesArr;

}- (void)viewDidLoad {

[super viewDidLoad];

UIImageView*gifImageView =[[UIImageView alloc] initWithFrame:frame];

gifImageView.animationImages= [self animationImages];//獲取Gif圖片列表gifImageView.animationDuration =5;//執(zhí)行一次完整動(dòng)畫(huà)所需的時(shí)長(zhǎng)gifImageView.animationRepeatCount =0;//動(dòng)畫(huà)重復(fù)次數(shù)[gifImageView startAnimating];

[self.view addSubview:gifImageView];

}

補(bǔ)充:可以直接取出動(dòng)態(tài)image。需要吧圖片設(shè)置進(jìn)去。從零開(kāi)始。

UIImage *image = [UIImage animatedImageNamed:@"youhuiquanxiao"duration:2];

3、使用SDWebImage

但是很遺憾,SDWebImage 的 sd_setImageWithURL:placeholderImage:這個(gè)方法是不能播放本地Gif的,它只能顯示Gif的第一張圖片而已。So,此方法行不通

// 行不通

UIImageView *gifImageView =[[UIImageView alloc] initWithFrame:frame];

[gifImageView sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"gifTest.gif"]];

其實(shí),在SDWebImage這個(gè)庫(kù)里有一個(gè)UIImage+GIF的類(lèi)別,里面為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 = [[NSBundle mainBundle] pathForResource:@"gifTest"ofType:@"gif"];

NSData*data =[NSData dataWithContentsOfFile:path];

UIImage*image =[UIImage sd_animatedGIFWithData:data];

gifImageView.image= image;

然后通過(guò)斷點(diǎn),我們發(fā)現(xiàn):

image的isa指針指向了_UIAnimatedImage ,說(shuō)明它是一個(gè)叫作_UIAnimatedImage 的類(lèi)(當(dāng)然,這個(gè)_UIAnimatedImage 蘋(píng)果是不會(huì)直接讓我們使用的)

_images 表示:這個(gè)Gif包含了多少?gòu)垐D片

_duration表示:執(zhí)行一次完整動(dòng)畫(huà)所需的時(shí)長(zhǎng)

其實(shí),動(dòng)畫(huà)執(zhí)續(xù)時(shí)間_duration也可以更改!

我們來(lái)看下此方法的內(nèi)部實(shí)現(xiàn):

+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {if(!data) {returnnil;

}

CGImageSourceRef source=CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);

size_t count=CGImageSourceGetCount(source);

UIImage*animatedImage;if(count <=1) {

animatedImage=[[UIImage alloc] initWithData:data];

}else{

NSMutableArray*images =[NSMutableArray array];

NSTimeInterval duration=0.0f;for(size_t i =0; i < count; i++) {

CGImageRef image=CGImageSourceCreateImageAtIndex(source, i, NULL);

duration+=[self sd_frameDurationAtIndex:i source:source];

[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];

CGImageRelease(image);

}if(!duration) {

duration= (1.0f/10.0f) *count;

}

animatedImage=[UIImage animatedImageWithImages:images duration:duration];

}

CFRelease(source);returnanimatedImage;

}

很明顯,duration是可以隨意更改的,只不過(guò)此方法設(shè)置了一個(gè)默認(rèn)值(duration = (1.0f / 10.0f) * count)

歸根到底,創(chuàng)建新的動(dòng)態(tài)的Image其實(shí)是調(diào)用了系統(tǒng)提供的一個(gè)UIImage的類(lèi)方法而已:

UIImage *animatedImage = [UIImage animatedImageWithImages:images duration: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文件下載下來(lái),格式為一個(gè)NSData

2、如果判斷是Gif格式,則會(huì)調(diào)用** sd_animatedGIFWithData:** 將Data轉(zhuǎn)換成我們需要的Gif格式

3、通過(guò)上面的方法二即可顯示出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地址即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Gif圖片是非常常見(jiàn)的圖片格式,尤其是在聊天的過(guò)程中,Gif表情使用地很頻繁。但是iOS竟然沒(méi)有現(xiàn)成的支持加載和播...
    iOS_大菜鳥(niǎo)閱讀 3,043評(píng)論 1 5
  • 1.系統(tǒng)UIImageView 多張圖片組成動(dòng)畫(huà) /** * UIImageView 動(dòng)畫(huà) * Memor...
    zhengelababy閱讀 8,912評(píng)論 3 6
  • 1. 原生方法: UIWebView特點(diǎn):加載速度略長(zhǎng),性能更優(yōu),播放的gif動(dòng)態(tài)圖更加流暢。 UIImagVie...
    奮斗的蝸牛閱讀 4,388評(píng)論 1 1
  • 1、通過(guò)拆分gif,加載一個(gè)圖片數(shù)組(推薦一個(gè)Mac APP自動(dòng)拆分gif:Gif Preview) 2、UIWe...
    薄涼_簡(jiǎn)書(shū)閱讀 564評(píng)論 0 1
  • Gif圖片是非常常見(jiàn)的圖片格式,尤其是在聊天的過(guò)程中,Gif表情使用地很頻繁。但是iOS竟然沒(méi)有現(xiàn)成的支持加載和播...
    CoderSC閱讀 2,490評(píng)論 0 0