【问题标题】:Resize Image with SDWebImage in UITableView在 UITableView 中使用 SDWebImage 调整图像大小
【发布时间】:2018-08-09 11:08:46
【问题描述】:

我在 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 frame] must be used only from main thread 从这一行:

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

我到底做错了什么?

【问题讨论】:

    标签: objective-c uitableview sdwebimage


    【解决方案1】:

    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;
    }
    

    【讨论】:

    • 没有任何改变
    • 我不知道在哪里调用这个方法,你能告诉我吗? -(UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL
    • 在自定义单元格中,在方法 awakeFromNib 中我调用了 SDWebImageManager.sharedManager.delegate = self; 并实现了 -(UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage :(UIImage *)image withURL:(NSURL *)imageURL
    • 我又有eidit回答了,希望能解决你的问题。
    • 有问题吗?
    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 2019-01-20
    • 1970-01-01
    相关资源
    最近更新 更多