在网上找到一个方法解决了这个问题,就是在SDWebImage 中有一个获取缓存的方法,先判断SDWebImage有没有缓存这个图片,有的话就获取,没有再使用
NSData *data = [NSData dataWithContentsOfURL:url];
image = [UIImage imageWithData:data];
来获取
代码如下,
//显示单张图片时,固定宽度。
__block CGFloat itemW = 200;
__block CGFloat itemH = 0;
if (_photoDataArray.count == 1) {
UIImageView * imageView = [[UIImageView alloc] init];
NSURL * url = [NSURL URLWithString:_photoDataArray.firstObject];
[imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder.jpg"]];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
BOOL existBool = [manager diskImageExistsForURL:url];//判断是否有缓存
UIImage * image;
if (existBool) {
image = [[manager imageCache] imageFromDiskCacheForKey:url.absoluteString];
}else{
NSData *data = [NSData dataWithContentsOfURL:url];
image = [UIImage imageWithData:data];
}
//根据image的比例来设置高度
if (image.size.width) {
itemH = image.size.height / image.size.width * itemW;
if (itemH >= itemW) {
itemW = 120;
itemH = image.size.height / image.size.width * itemW;
}
}