【问题标题】:How to resize SDWebImage before Cache, considering UIImageView size如何在缓存之前调整 SDWebImage 的大小,考虑 UIImageView 的大小
【发布时间】:2017-11-01 21:48:47
【问题描述】:

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

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

【问题讨论】:

    标签: ios caching resize sdwebimage


    【解决方案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")
                }
            }
        })
    

    【讨论】:

    • 我是这么想的,但我不知道如何用 SDWebImage 实现它。我会试试你的版本,似乎是正确的,非常感谢!
    • 对不起,回答晚了,但我终于试了一下,似乎效果很好:) 非常感谢!
    【解决方案2】:

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

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

    }

    【讨论】:

    • 这几乎正是我已经在做的事情,问题是原始图像被缓存了不是吗?我想缓存调整大小的图像,所以我不必一次又一次地调用调整大小函数
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多