iOS加载网络GIF图片和本地图片

现在的APP不单单是讲究功能实在,还是得看颜值.
下面记录一下我最近在做一个显示GIF的功能列表的时候,遇到了一些小坑.
测试demo

主要用到的技术:
  • SDWebImage
  • AssetsLibrary.framework(系统自带的第三方类库)
  • YYWebImage

我们做iOS开发的,一般遇到加载图片,都会想到SDWebImage这个第三方类库,然而这个第三方类库我在实现对应的代码逻辑的时候,发现并不是很好处理网络加载URLgif图片格式.

1.SDWebImage
  • 传统的加载
     //传统意义的加载
     [imgView sd_setImageWithURL:url placeholderImage:nil completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
     imgView.image = image;
     }];
  • 加载本地
//1. 加载本地的gif图片
     FLAnimatedImageView *imgView = (FLAnimatedImageView*)[cell.contentView viewWithTag:1];
     imgView.contentMode = UIViewContentModeScaleAspectFit;
     
     NSString * bundlePath = [[ NSBundle mainBundle] pathForResource: @ "gifBundle" ofType :@ "bundle"];
     NSString *imgPath= [bundlePath stringByAppendingPathComponent :[NSString stringWithFormat:@"/%ld.gif", indexPath.row+1]];
     
     NSData  *imageData = [NSData dataWithContentsOfFile:imgPath];
     
     imgView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];

在调试的时候,发行加载本地的没有问题,也没有重用的现象出现,也没有卡顿.所以我们就打算采用这个加载网络URL的GIF图片.

  • 加载url的gif
 //2. 加载url gif 图片 (需要在故事板设置imageview的类型为FLAnimatedImageView)
     FLAnimatedImageView *imageView = (FLAnimatedImageView*)[cell.contentView viewWithTag:1];
    NSDictionary *dic = listArray[indexPath.row];
    NSURL *url = [NSURL URLWithString:[dic valueForKey:@"GIFImageURL"]];
    imageView.image = [UIImage imageNamed:@"专注.jpg"];
    
    [self loadAnimatedImageWithURL:url completion:^(FLAnimatedImage *animatedImage) {
        imageView.animatedImage = animatedImage;
    }];

异步加载

- (void)loadAnimatedImageWithURL:(NSURL *const)url completion:(void (^)(FLAnimatedImage *animatedImage))completion
{
    NSString *const filename = url.lastPathComponent;
    NSString *const diskPath = [NSHomeDirectory() stringByAppendingPathComponent:filename];
    
    NSData * __block animatedImageData = [[NSFileManager defaultManager] contentsAtPath:diskPath];
    FLAnimatedImage * __block animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:nil];
    
    if (animatedImage) {
        if (completion) {
            completion(animatedImage);
        }
    } else {
        [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            animatedImageData = data;
            animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:animatedImageData];
            if (animatedImage) {
                if (completion) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        completion(animatedImage);
                    });
                }
                [data writeToFile:diskPath atomically:YES];
            }
        }] resume];
    }
}

我们发现,按照他们的使用方法来实现的时候,发行出现稍微卡顿和重用的bug.但是我们还没放弃,我们想按照加载本地的方法来加载试试.
这个时候引入类库:AssetsLibrary

FLAnimatedImageView *imgView = (FLAnimatedImageView*)[cell.contentView viewWithTag:1];
    imgView.contentMode = UIViewContentModeScaleAspectFit;

    ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];


    void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *) = ^(ALAsset *asset) {
        if (asset != nil) {

            ALAssetRepresentation *rep = [asset defaultRepresentation];
            Byte *imageBuffer = (Byte*)malloc(rep.size);
            NSUInteger bufferSize = [rep getBytes:imageBuffer fromOffset:0.0 length:rep.size error:nil];
            NSData *imageData = [NSData dataWithBytesNoCopy:imageBuffer length:bufferSize freeWhenDone:YES];
            FLAnimatedImage *i =  [FLAnimatedImage animatedImageWithGIFData:imageData];

            imgView.animatedImage = i;
        }
        else {
        }
    };

    [assetLibrary assetForURL:url
                  resultBlock:ALAssetsLibraryAssetForURLResultBlock
                 failureBlock:^(NSError *error) {

                 }];

    */

结果发现可以加载出来,但是GIF图片不动!!
所以我们上网查找了相关资料,发行了使用YYWebImage效果更好,更加容易实现.

PS:为什么使用FLAnimatedImageView,是因为SDWebImage之前的UIImage+GIF不在处理gif,而是交给FLAnimatedImage来处理
具体导入直接 pod 'SDWebImage/GIF'即可
FLAnimatedImage源代码

2.YYWebImage

YYWebImage使用起来很简单
pod 'YYWebImage'
在使用界面引入#import "YYWebImage.h"即可

    /*
     //YYWebImage 使用
     YYAnimatedImageView *imageView = (YYAnimatedImageView*)[cell.contentView viewWithTag:1];
     imageView.yy_imageURL = url;
     */

其中还有很多的加载方式和样式,大家有时间可以去尝试.
参考资源:

  1. SDWebImage播放GIF存在的问题,以及解决办法
  2. iOS 几种图片或Gif异步加载
  3. iOS开发gif图片转成NSData
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,223评论 4 61
  • 一个月又过去了一半了,时间过的很快,八月八日晚上九寨沟地震了,四川这个美丽得天府之国,又一次的给我们带来了震撼汶川...
    遇见继承阅读 921评论 0 0
  • 作为独生一代中稀有的"大孩儿",对现在大孩儿很不理解,虽然一开始多了个弟弟会不适应,但绝不会考虑你死我生的问题。父...
    嘿别懒了阅读 3,251评论 0 1
  • 今天是九月五日,开学三天了。 相信很多人在高中那种痛苦的生活中,大学生活是熬过那些苦日子的动力。因为我也是...
    成成亦可阅读 1,586评论 0 0
  • 十一月十九日墨尔本晴 最近晚上从图书馆回来就困的不行,很多时候回来倒头就睡了,一觉醒来,外面的鸟都在叽叽喳喳的了,...
    青橙miss橙阅读 2,999评论 0 0