探究:SDWebImage,相同url,图片不同的情况
2022-07-20 本文已影响0人
失忆的程序员
![](https://img.haomeiwen.com/i1443029/73c719a61c361850.jpg)
先加载默认图片,再从后台下载来替代
#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];
关于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属性
[self.HeaderImage sd_setImageWithURL:[NSURL URLWithString:yhPic] placeholderImage:[UIImage imageNamed:@"ameng_dh"] options:SDWebImageRefreshCached];
SDWebImageRefreshCached是专门用来处理相同url,图片不同的情况的。
这两种写法根据情况而定.