考虑到 UIImageView 大小,如何在缓存之前调整 SDWebImage 的大小

How to resize SDWebImage before Cache, considering UIImageView size

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

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

获取图片后可以调整大小。只需传递图像和图像的高度和宽度。

[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{

                }
            }];

下面是调整图片大小的函数。

-(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];

}

这比 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")
            }
        }
    })