SDWebImage的使用及圖片不改變問題的解決


對於基本使用,先導入

#import "UIImageView+WebCache.h"

①先加載默認圖片,再從后台下載來替代

UIImage *defaultImg = [UIImage imageNamed:@"defaultimage"]; NSString *urlStr = @"http://n.sinaimg.cn/edu/transform/20160505/pe7k-fxryhhu2274915.png"; [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:defaultImg];

②SDWebImage默認是有緩存的。緩存時間是1周

static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week

查找url對應的圖片緩存可以用,key為對應的url

UIImage *defaultImg = [UIImage imageNamed:@"defaultimage"]; NSString *urlStr = @"http://n.sinaimg.cn/edu/transform/20160505/pe7k-fxryhhu2274915.png"; //查找對應的圖片緩存,key為url
UIImage *originalImg = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urlStr]; if (originalImg) { imgView.image = originalImg; } else { [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:defaultImg]; }

③獲取所有圖片緩存,並清除

//獲取緩存,getSize直接獲取
-(void)findAllImageCache{ NSUInteger imgSize = [[SDImageCache sharedImageCache] getSize]; NSString * currentVolum = [NSString stringWithFormat:@"%@",[self fileSizeWithInterge:imgSize]]; NSString *msg = [NSString stringWithFormat:@"緩存為%@",currentVolum]; }
// 轉換大小
- (NSString *)fileSizeWithInterge:(NSInteger)size{ // 1k = 1024b, 1m = 1024k
    if (size < 1024) {// 小於1k
        return [NSString stringWithFormat:@"%ldB",(long)size]; }else if (size < 1024 * 1024){// 小於1m
        CGFloat aFloat = size/1024; return [NSString stringWithFormat:@"%.0fK",aFloat]; }else if (size < 1024 * 1024 * 1024){// 小於1G
        CGFloat aFloat = size/(1024 * 1024); return [NSString stringWithFormat:@"%.1fM",aFloat]; }else{ CGFloat aFloat = size/(1024*1024*1024); return [NSString stringWithFormat:@"%.1fG",aFloat]; } }
//清除緩存
[[SDImageCache sharedImageCache] clearDisk];

 

在tableview中需要注意的問題

- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;

該方法是從后台獲取圖片,當下次請求時會首先查看緩存中是否有圖片,若有,則優先加載緩存而不是直接下載;沒有的話再從后台獲取。所以在一些uitableview上,cell上有圖片,由於存在cell復用,並且sdwebimage有緩存,按照正常邏輯來說是

UIImage *defaultImg = [UIImage imageNamed:@"defaultimage"]; NSString *urlStr = @"http://n.sinaimg.cn/edu/transform/20160505/pe7k-fxryhhu2274915.png"; //查找對應的圖片緩存
UIImage *originalImg = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:urlStr]; if (originalImg) { imgView.image = originalImg; } else { [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:defaultImg]; }

這樣可能出現圖片引用混亂問題,所以為了解決該問題,直接使用sd_setImageWithURL即可,因為該方法會優先使用圖片緩存而不是直接下載。

UIImage *defaultImg = [UIImage imageNamed:@"defaultimage"]; NSString *urlStr = @"http://n.sinaimg.cn/edu/transform/20160505/pe7k-fxryhhu2274915.png"; [imgView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:defaultImg];

 

4.關於SDWebImage的圖片改變不了的問題

該SDWebImage的版本是3.7.2,app有個修改頭像的功能,成功后將原有圖像替換。但遇到的問題是,替換頭像后再次刷新,會返回另外一張圖片,雖然頭像的url是正確的,但顯示的圖片卻是錯誤的。查了stackoverflow,有說將其版本換為3.7.1就能解決的,但我換了后仍會出現該問題。
有在下列代碼之前加上__userFaceLogo.image = nil的,但都沒解決。

[_userFaceLogo sd_setImageWithURL:[NSURL URLWithString:userDto.userImgUrl] placeholderImage:[UIImage imageNamed:@"head portrait"]];

最后換了一種方法,不用SDWebimage,就好了

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:userDto.userImgUrl]]; _userFaceLogo.image = [UIImage imageWithData:data];

更新:找出問題所在了,因為修改頭像后,圖片的url是不變的,而默認情況下,SDWebimage對相同url是優先使用緩存的,因此得加options屬性

[_userFaceLogo sd_setImageWithURL:[NSURL URLWithString:userDto.userImgUrl] placeholderImage:[UIImage imageNamed:@"head portrait"] options:SDWebImageRefreshCached];

SDWebImageRefreshCached是專門用來處理相同url,圖片不同的情況的。

這兩種寫法根據情況而定。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM