美文网首页iOS开发iOS学习iOS 开发每天分享优质文章
SDWebImage4.0.0 加载本地gif之空白问题小结

SDWebImage4.0.0 加载本地gif之空白问题小结

作者: 凉风起君子意如何 | 来源:发表于2017-02-22 16:11 被阅读6435次

问题主要点:

  • 项目之前是采用如下方案加载gif文件(sd_animatedGIFWithData:imageData)
    UIImageView *imgView = [UIImageView new];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.frame = CGRectMake(30, 20, w, h);
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    imgView.backgroundColor = [UIColor clearColor];
    imgView.image = [UIImage sd_animatedGIFWithData:imageData];
    [loadingView addSubview:imgView];
  • 伙伴那边更新了Podfile文件,今天本地pod update,发现关于SDWebImage报了两个错误(一个是api多了参数,一个是api更新,找到错误之后,顺藤摸瓜就能解决),错误解决之后,发现gif动画加载是空白的。最先考虑是不是gif丢失了 导致data为空,断点发现不是,初步猜想应该是更新之后api改变了。后面就是一系列的猜想认证,伙伴是可以的,让他也更新pod,发现出来跟我一样的问题,进一步证实了我的猜想,既然知道是api更新导致的问题,那接下里就好办了。


    可以发现SDWebImage之前是3.8.2,现在最新的版本是4.0.0
  • github上搜索SDWebImage,会发现如下提示,哦,原来是这样的😝

    原来4.0之后,就FLAnimatedImageView这个代替了,用pod 'SDWebImage/GIF'导入这个库使用就行了
  • 改了之后的代码如下:

    FLAnimatedImageView *imgView = [FLAnimatedImageView new];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.frame = CGRectMake(30, 20, w, h);
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    imgView.backgroundColor = [UIColor clearColor];
    imgView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
    [loadingView addSubview:imgView];

补充:

网上大概搜了下加载本地gif的几种方法,其实也可以用UIWebview,代码如下:

    NSData *data = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"]];
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(30, 20, w, h)];
    webView.contentMode = UIViewContentModeScaleAspectFit;
    [webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [loadingView addSubview:webView];

但是苦于不能等比例缩放gif图片大小,大概要用到js来控制,就舍弃了,还是用了SDWebImage,这个以后补充更新。

end

相关文章

网友评论

    本文标题:SDWebImage4.0.0 加载本地gif之空白问题小结

    本文链接:https://www.haomeiwen.com/subject/nirmwttx.html