在UITableView中使用SDWebImage调整图像大小

时间:2018-08-01 14:16:58

标签: objective-c uitableview sdwebimage

我在UITableView中使用SDWebImage。 我需要根据图像帧创建像元大小,并且正在使用此方法:

-(UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL{



    CGSize newSize = CGSizeMake(self.cmImage.frame.size.width, self.cmImage.frame.size.width * image.size.height / image.size.width);
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);

    // draw in new context, with the new size
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    // End the context
    UIGraphicsEndImageContext();

    return newImage;

}

我在

上称呼它
- (void)awakeFromNib {
    SDWebImageManager.sharedManager.delegate = self;
}

并在 cellForRowAtIndexPath 上做到了:

 [SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:_model.data] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            self.cmImage.image = image;
        }];

调整图像大小并更改单元格中cmImage的帧

但是我认为此方法对于创建cmImage帧响应太迟,并且在滚动tableView之后某些图像正在调整大小,对于某些单元格我加载了不同的图像

我也收到警告此行只能从主线程使用[UIView框架]

CGSize newSize = CGSizeMake(self.cmImage.frame.size.width, self.cmImage.frame.size.width * image.size.height / image.size.width);

我到底在做什么错?

1 个答案:

答案 0 :(得分:0)

SDWebImage在后台线程中下载图像。所以:

[SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:_model.data] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {  
 dispatch_async(dispatch_get_main_queue(), ^{
        self.cmImage.image = image;
    });
}];

经常调用 cellForRowAtIndexPath 。因此,您应该在iamgeview上设置占位符大小。图像下载完成后调整imageview的大小。

-

----------------------再次编辑-------------------- -----

-

查看SDWebImage原始注释,此方法从全局队列中调用。因此,应在主队列中更改图像大小。

/**
 * Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory.
 * NOTE: This method is called from a global queue in order to not to block the main thread.
 *
 * @param imageManager The current `SDWebImageManager`
 * @param image        The image to transform
 * @param imageURL     The url of the image to transform
 *
 * @return The transformed image object.
 */
- (nullable UIImage *)imageManager:(nonnull SDWebImageManager *)imageManager transformDownloadedImage:(nullable UIImage *)image withURL:(nullable NSURL *)imageURL;

因此请为该方法添加以下代码

-(UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL{

    __block UIImage *newImage = nil ;
    dispatch_async(dispatch_get_main_queue(), ^{
        CGSize newSize = CGSizeMake(self.cmImage.frame.size.width, self.cmImage.frame.size.width * image.size.height / image.size.width);
        // Create a graphics image context
        UIGraphicsBeginImageContext(newSize);

        // draw in new context, with the new size
        [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];

        // Get the new image from the context
         newImage = UIGraphicsGetImageFromCurrentImageContext();

        // End the context
        UIGraphicsEndImageContext();

    });
        return newImage;
}