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,图片不同的情况的。

这两种写法根据情况而定。

 

转载于:https://www.cnblogs.com/Apologize/p/5502234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值