iOS 顯示動(dòng)態(tài)圖、GIF圖方法總結(jié)

總結(jié)了iOS中顯示動(dòng)態(tài)圖的幾種方法。點(diǎn)擊下載 Demo

一、WebView加載

可以通過WebView加載本地Gif圖和網(wǎng)絡(luò)Gif圖,但圖片大小不能自適應(yīng)控件大小,也不能設(shè)置Gif圖播放時(shí)間。使用如下:

// 1、WebView加載
- (void)webViewShowGif {
    UIWebView *webView = self.viewArr[0];
    
    // 本地地址
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"gif"];
    // 網(wǎng)路地址
//    NSString *imagePath = @"http://qq.yh31.com/tp/zjbq/201711092144541829.gif";
    
    NSURL *imageUrl = [NSURL URLWithString:imagePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];
    [webView loadRequest:request];
}

二、UIImageView加載多圖動(dòng)畫

把動(dòng)態(tài)圖拆分成一張張圖片,將一系列幀添加到animationImages數(shù)組里面,然后設(shè)置animation一系列屬性,如動(dòng)畫時(shí)間,動(dòng)畫重復(fù)次數(shù)。例:

// 2、UIImageView加載多張圖片,播放
- (void)imageViewStartAnimating {
    UIImageView *imageView = self.viewArr[1];
    
    NSMutableArray *imageArr = [NSMutableArray arrayWithCapacity:3];
    for (int i = 0; i<3; i++) {
        NSString *imageStr = [NSString stringWithFormat:@"import_progress%d",i + 1];
        UIImage *image = [UIImage imageNamed:imageStr];
        [imageArr addObject:image];
    }
    imageView.animationImages = imageArr;
    imageView.animationDuration = 2;
    [imageView startAnimating];
}

三、SDWebImage加載本地GIF

在SDWebImage這個(gè)庫(kù)里有一個(gè)UIImage+GIF的類別,使用sd_animatedGIFWithData方法可以將GIF圖片數(shù)據(jù)專為圖片。例:

// 3、SDWebImage加載本地GIF
- (void)imageViewLocalGif {
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"happy" ofType:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
    UIImage *image = [UIImage sd_animatedGIFWithData:imageData];

    UIImageView *imageView = self.viewArr[2];
    imageView.image = image;
}

四、SDWebImage加載網(wǎng)絡(luò)GIF

首先將網(wǎng)絡(luò)gif圖下載到本地,然后再用sd_animatedGIFWithData方法,轉(zhuǎn)為可用的圖片,下載gif圖的方式有兩種

方式一:采用SDWebImageDownloader下載,回調(diào)里面會(huì)有NSData。只是,你會(huì)發(fā)現(xiàn)采用SDWebImageDownloader下載,界面顯示就是沒有sd_setImageWithURL方法流暢,這是因?yàn)閟d_setImageWithURL里面對(duì)cache和線程做了很多處理,保證了UI的流暢。

    NSString *imageStr = @"http://qq.yh31.com/tp/zjbq/201711142021166458.gif";
    NSURL *imgeUrl = [NSURL URLWithString:imageStr];
    SDWebImageDownloaderOptions options = 0;
    UIImageView *imageView = self.viewArr[3]; 
    
    // 方法一 SDWebImageDownloader下載
    SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
    [downloader downloadImageWithURL:imgeUrl
                             options:options
                            progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {

                            } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                                imageView.image = [UIImage sd_animatedGIFWithData:data];
                            }];

方式二、sd_setImageWithURL下載,回調(diào)的時(shí)候不用image,去直接讀cache。(首先要了解sd_setImageWithURL里的內(nèi)部邏輯,下載完之后先入cache,再執(zhí)行block,這才保證外面可以直接讀取到),取出來的就是NSData。首次下載成功時(shí),可能獲取data失敗,因?yàn)檫@次圖片可能還沒存儲(chǔ)成功,有延遲。

    // 方法二 sd_setImageWithURL下載
    SDWebImageOptions opt = SDWebImageRetryFailed | SDWebImageAvoidAutoSetImage;
    [imageView sd_setImageWithURL:imgeUrl
                 placeholderImage:nil
                          options:opt
                        completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                            
                            if (image.images && image.images.count) {
                                NSString *path = [[SDImageCache sharedImageCache] defaultCachePathForKey:imageURL.absoluteString];
                                NSData *data = [NSData dataWithContentsOfFile:path];
                                UIImage *gifImage = [UIImage sd_animatedGIFWithData:data];
                                imageView.image = gifImage;
                            }
                        }];

五、FLAnimatedImage使用

FLAnimatedImage 是由Flipboard開源的iOS平臺(tái)上播放GIF動(dòng)畫的一個(gè)優(yōu)秀解決方案,在內(nèi)存占用和播放體驗(yàn)都有不錯(cuò)的表現(xiàn)。FLAnimatedImage項(xiàng)目的流程比較簡(jiǎn)單,F(xiàn)LAnimatedImage就是負(fù)責(zé)GIF數(shù)據(jù)的處理,然后提供給FLAnimatedImageView一個(gè)UIImage對(duì)象。FLAnimatedImageView拿到UIImage對(duì)象顯示出來就可以了。 例:

// 5、FLAnimatedImage使用
- (void)animatedImageViewShowGif {
    FLAnimatedImageView *imageView = self.viewArr[4];
    
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"weiwei" withExtension:@"gif"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:data];
    imageView.animatedImage = animatedImage;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容