SDWebImage4.0.0 加載本地gif之空白問題小結

問題主要點:

  • 項目之前是采用如下方案加載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,發(fā)現(xiàn)關于SDWebImage報了兩個錯誤(一個是api多了參數(shù),一個是api更新,找到錯誤之后,順藤摸瓜就能解決),錯誤解決之后,發(fā)現(xiàn)gif動畫加載是空白的。最先考慮是不是gif丟失了 導致data為空,斷點發(fā)現(xiàn)不是,初步猜想應該是更新之后api改變了。后面就是一系列的猜想認證,伙伴是可以的,讓他也更新pod,發(fā)現(xiàn)出來跟我一樣的問題,進一步證實了我的猜想,既然知道是api更新導致的問題,那接下里就好辦了。


    可以發(fā)現(xiàn)SDWebImage之前是3.8.2,現(xiàn)在最新的版本是4.0.0
  • github上搜索SDWebImage,會發(fā)現(xiàn)如下提示,哦,原來是這樣的??

    原來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];

補充:

網(wǎng)上大概搜了下加載本地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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容