2017-05-31 148 views
0

我正在使用SDWebImage加载和缓存我加载到我的tableViewCells中的图像。我想调整图像大小以获得更好的滚动性能,并为UIImageView编写了resize函数作为扩展,将图像大小调整为imageView的大小。搜索解决方案我找到了SDWebImageManager函数,可以在缓存它之前调整图像的大小,唯一的问题是该参数是UIImage,所以我无法调整它的大小以适应它所在的ImageView。我希望你大家明白我的意思。这就是我目前的做法,这对性能来说有点不好,因为每次显示单元格时都会调整大小。大小当然是ImageView的大小。如何在缓存之前调整SDWebImage的大小,考虑UIImageView的大小

cell.firstImage.sd_setImage(with: URL(string: image0Uri), completed: { (img, err, cache, url) in 
        cell.firstImage.resize(toSize: size) 
       }) 

回答

0

这有点不仅仅是1行代码比较复杂,您可能需要手动加载的图像,然后调整其大小,然后将其保存用自己的密钥缓存,然后取与低熔酚醛你的关键

也许是这样的:

SDImageCache.shared().queryCacheOperation(forKey: "KEY", done: { (image, data, type) in 
     if let image = image { 
      self.firstImage.image = image 
     } else { 
      if let imgUrl = url { 
       SDWebImageManager.shared().loadImage(with: URL(string: imgUrl), options: [], progress: nil) { (image, data, error, type, done, url) in 
        image.resize(toSize: size) 
        self.firstImage.image = image 
        SDImageCache.shared().store(image, forKey: "KEY", completion: nil) 
       }) 
      } else { 
       self.firstImage.image = UIImage(named: "img_default") 
      } 
     } 
    }) 
+0

这就是我的想法,但我不知道如何实现它与SDWebImage ..我会尝试你的版本,似乎是正确的,非常感谢! –

+0

对不起,对于迟到的答案,但我终于尝试了它,它似乎完美:)非常感谢! –

0

您可以在获取它之后调整图像大小。只是通过图像和图像的高度和宽度。

[cell.imgview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",Image_URL2]] placeholderImage:[UIImage imageNamed:@"imagename"] completed:^(UIImage image, NSError error, SDImageCacheType cacheType, NSURL *imageURL) { 
       if (image) { 
        cell.imgview.image=[Utility compressImageDynamic:image hight:(360 [UIScreen mainScreen].bounds.size.width)/320 width:(360 [UIScreen mainScreen].bounds.size.width)/320]; 

       } 
       else{ 

       } 
      }]; 

以下是reSize图像的函数。

-(UIImage)compressImageDynamic:(UIImage)image hight:(float)x width:(float)y{ 
float actualHeight = image.size.height; 
float actualWidth = image.size.width; 
float maxHeight = x; 
float maxWidth = y; 
float imgRatio = actualWidth/actualHeight; 
float maxRatio = maxWidth/maxHeight; 
float compressionQuality = 1;//50 percent compression 

if (actualHeight > maxHeight || actualWidth > maxWidth){ 
    if(imgRatio < maxRatio){ 
     //adjust width according to maxHeight 
     imgRatio = maxHeight/actualHeight; 
     actualWidth = imgRatio * actualWidth; 
     actualHeight = maxHeight; 
    } 
    else if(imgRatio > maxRatio){ 
     //adjust height according to maxWidth 
     imgRatio = maxWidth/actualWidth; 
     actualHeight = imgRatio * actualHeight; 
     actualWidth = maxWidth; 
    } 
    else{ 
     actualHeight = maxHeight; 
     actualWidth = maxWidth; 
    } 
}else{ 
    actualHeight = maxHeight; 
    actualWidth = maxWidth; 
    compressionQuality = 1; 
} 

CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight); 
UIGraphicsBeginImageContext(rect.size); 
[image drawInRect:rect]; 
UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); 
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality); 
UIGraphicsEndImageContext(); 

return [UIImage imageWithData:imageData]; 

}

+0

那几乎正是我已经在做,问题是原始图像缓存,不是吗?我想缓存调整大小的图像,所以我不必一次又一次地调用resize函数 –