iOS加載GIF圖

1.WebView

UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:webView]; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSURL *url = [NSURL URLWithString:path]; // 可以直接是網(wǎng)絡(luò)URL
[webView loadRequest:[NSURLRequest requestWithURL:url]];
UIWebView實(shí)現(xiàn)代碼截圖
WKWebView

WKWebView實(shí)習(xí)和UIWebView一樣,也是將GIF圖片的URL加載出來(lái)

WKWebView代碼實(shí)現(xiàn)

如果 [[NSBundle mainBundle] pathForResource:@"" ofType:@""] 無(wú)法獲取到文件
將一個(gè)文件導(dǎo)入到工程中后,用[[NSBundle mainBundle] pathForResource:@"" ofType:@""]來(lái)獲取到該文件時(shí),一直無(wú)法拿到這個(gè)文件,解決方法如下

在Build Phases -> Copy Bundle Resources下點(diǎn)擊加號(hào)(+)

2.SDWebImage

4.0.0以前

SDWebImage 提供了一個(gè)分類(lèi) 'UIImage+GIF' 來(lái)實(shí)現(xiàn)加載GIF圖
不能直接使用sd_setImageWithURL:placeholderImage . 這個(gè)方法是不能播放本地Gif的,它只能顯示Gif的第一張圖片而已。

UIImage+GIF.h

@interface UIImage (GIF)
+ (UIImage *)sd_animatedGIFNamed:(NSString *)name;
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)size;
@end

本地GIF圖代碼實(shí)現(xiàn)

NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage sd_animatedGIFWithData:data];
self.imageView.image = image;

網(wǎng)絡(luò)GIF圖 代碼實(shí)現(xiàn)

NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503467551868&di=f53202da5f5fbd6799c38da661d35ec6&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F160622%2F2-1606221R445U9.gif"];
SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger               
        expectedSize, NSURL * _Nullable targetURL) {
        //下載進(jìn)度
} completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error,
        BOOL finished) {
        UIImage *gifimage = [UIImage sd_animatedGIFWithData:data];
        self.imageView.image = gifimage;
}];
4.0.0以上

SDWebImage 在4.0版本 更改加載GIF圖片的使用方法,修改了 UIImage+'GIF' 分類(lèi)
將原有的三個(gè)方法刪除掉兩個(gè), sd_animatedGIFWithData 也不能實(shí)現(xiàn)加載出GIF圖片了,
新加方法 isGIF 判斷圖片是否是GIF圖 (原理:判斷UIImage的images參數(shù)是否為空)
UIImage+GIF.h

/**
 *  Compatibility method - creates an animated UIImage from an NSData, it will only contain the 1st frame image
* 翻譯: 兼容性方法 - 從NSData創(chuàng)建動(dòng)畫(huà)UIImage,它只包含第一幀圖像
 */
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data;

/**
 *  Checks if an UIImage instance is a GIF. Will use the `images` array
* 翻譯: 檢查UIImage實(shí)例是否是GIF。 將使用`images`數(shù)組
 */
- (BOOL)isGIF;

從4.0版本開(kāi)始 推出了 FLAnimatedImage 來(lái)實(shí)現(xiàn)加載GIF圖
如果您使用Cocapods,請(qǐng)將 pod'SDWebImage / GIF' 添加到您的podfile中。
要使用它,只需確保使用FLAnimatedImageView而不是UIImageView。
注意:有一個(gè)向后兼容的功能,所以如果您仍然嘗試將GIF加載到UIImageView中,它將僅將第一幀顯示為靜態(tài)圖像。 詳見(jiàn):SDwebImage

FLAnimatedImage 使用

import <FLAnimatedImageView+WebCache.h>

FLAnimatedImageView 加載本地GIF圖

FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
[self.view addSubview:imageView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
imageView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];

加載網(wǎng)絡(luò)GIF圖

FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
imageView.center = self.view.center;
[self.view addSubview:imageView];
NSURL *url = [NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503467551868&di=f53202da5f5fbd6799c38da661d35ec6&imgtype=0&src=http%3A%2F%2Fscimg.jb51.net%2Fallimg%2F160622%2F2-1606221R445U9.gif"];
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"test"]];

其中 animatedImage 是 FLAnimatedImage 的實(shí)例對(duì)象
導(dǎo)入 <FLAnimatedImageView+WebCache.h> 后 FLAnimatedImageView 加載URL GIF圖片 與UIImageView加載URL圖片并無(wú)太大區(qū)別

優(yōu)缺點(diǎn):

SDWebImage

容器都是大家熟悉的UIImageView,方便操作,還可以設(shè)置ContentModel
4.0以前還能是設(shè)置GIF圖的duration (進(jìn)入.m 找到關(guān)鍵字修改值即可)
4.0以后: 推出 FLAnimatedImage 類(lèi),擁有了更多的屬性

4.0以前sd_animatedGIFWithData中duration的設(shè)定,這里是一個(gè)默認(rèn)值,有需要可以修改
FLAnimatedImage.h 部分截圖
webView

不能設(shè)置duration,也無(wú)法獲取GIF圖片的參數(shù)
容器為不常用的UIWebView 或者 WKWebView
無(wú)法更改圖片的顯示樣式,只能改變控件的大小
方便快捷,不需要依賴第三方

建議:
有g(shù)if圖,能給一個(gè)App添彩不少,但是我到目前為止還沒(méi)有發(fā)現(xiàn)哪一個(gè)App中有大量的gif圖展示的,一般項(xiàng)目中不會(huì)有大量的gif圖存在(一些主要目的就是展示gif圖的App除外),我自己的項(xiàng)目中也只是個(gè)別地方用到, GIF圖一旦過(guò)多,肯定是影響性能的
簡(jiǎn)單快捷時(shí) 可以是使用webview ,需要操作GIF圖時(shí)
建議使用 SDWebImage, 畢竟是一個(gè)大多數(shù)項(xiàng)目都依賴的第三方庫(kù)
UITablView 建議使用 SDWebImage

結(jié)尾:

加載GIF圖還有很多種方式,
例如直接使用原生框架<ImageIO>來(lái)實(shí)現(xiàn), 太過(guò)復(fù)雜
CADisplayLink / CAKeyframeAnimation 都能實(shí)現(xiàn),就是太過(guò)于麻煩
GIF圖拆分 使用序列幀也可以 太過(guò)于耗性能
如果大家項(xiàng)目中需要用到序列幀來(lái)實(shí)現(xiàn)加載動(dòng)畫(huà) 建議使用:lottie-ios

最后編輯于
?著作權(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)容