日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 加载本地Gif图片

發(fā)布時間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 加载本地Gif图片 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

記錄一個iOS 加載本地gif格式圖片的方法


項目中需要加載本地gif圖片,一開始使用的是SDWebImage框架的

  • (nullable UIImage *)sd_imageWithGIFData:(nullable NSData *)data;

但是在部分機型上出現(xiàn)了gif不能正常顯示的問題(這里是iPhone 6s,iOS 12.4), 然后更換加載方式,使用騰訊的QMUIKit框架進行加載,使用方法

  • (UIImage *)qmui_animatedImageWithData:(NSData *)data scale:(CGFloat)scale

發(fā)現(xiàn)還是無法正常顯示, 于是查看了qmui_animatedImageWithData:(NSData *)data scale:(CGFloat)scale的實現(xiàn)源碼

  • (UIImage *)qmui_animatedImageWithData:(NSData *)data scale:(CGFloat)scale {
    // http://www.jianshu.com/p/767af9c690a3
    // https://github.com/rs/SDWebImage
    if (!data) {
    return nil;
    }
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    size_t count = CGImageSourceGetCount(source);
    UIImage *animatedImage = nil;
    if (count <= 1) {
    animatedImage = [[UIImage alloc] initWithData:data];
    } else {
    NSMutableArray<UIImage *> *images = [[NSMutableArray alloc] init];
    NSTimeInterval duration = 0.0f;
    for (size_t i = 0; i < count; i++) {
    CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
    duration += [self qmui_frameDurationAtIndex:i source:source];
    UIImage *frameImage = [UIImage imageWithCGImage:image scale:scale == 0 ? ScreenScale : scale orientation:UIImageOrientationUp];
    [images addObject:frameImage];
    CGImageRelease(image);
    }
    if (!duration) {
    duration = (1.0f / 10.0f) * count;
    }
    animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }
    CFRelease(source);
    return animatedImage;
    }

查看了注釋中的簡書和github,最后采用如下方式加載

// #import "UIImageView+Gif.h" //給imageView添加category- (void)at_setGifImageWithGifFile:(NSString *)file animationDuration: (CGFloat)duration{NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:file withExtension: @"gif"]; //加載GIF圖片CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef) fileUrl, NULL); //將GIF圖片轉(zhuǎn)換成對應(yīng)的圖片源size_t frameCout = CGImageSourceGetCount(gifSource); //獲取其中圖片源個數(shù),即由多少幀圖片組成NSMutableArray *frames = [[NSMutableArray alloc] init]; //定義數(shù)組存儲拆分出來的圖片for (size_t i = 0; i < frameCout; i++) {CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //從GIF圖片中取出源圖片UIImage *imageName = [UIImage imageWithCGImage:imageRef]; //將圖片源轉(zhuǎn)換成UIimageView能使用的圖片源[frames addObject:imageName]; //將圖片加入數(shù)組中CGImageRelease(imageRef);}CFRelease(gifSource);self.animationImages = frames; //將圖片數(shù)組加入UIImageView動畫數(shù)組中self.animationDuration = duration; //每完成一次動畫時長[self startAnimating]; //開啟動畫}

這樣使用在UITableView或者UICollection等列表中加載可能會出現(xiàn)問題,可嘗試以下方法解決

if (!<#imageView#>.isAnimating) {[<#imageView#> startAnimating]; }

參考文檔:
簡書、github

總結(jié)

以上是生活随笔為你收集整理的iOS 加载本地Gif图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。