OGeek|极客世界-中国程序员成长平台

标题: ios - setImageWithURLRequest 加载成功,但显示相同的图像 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:33
标题: ios - setImageWithURLRequest 加载成功,但显示相同的图像

每当我需要更改正在调用的图像时,我在表格 View 中有一个包含图像的单元格(行):

-(void)loadThumbnailWithPathNSString *)path {
    NSLog(@"loadThumbnailWithPath:%@",path);
    UIImage* placeholder = [UIImage imageNamed"ic_courselist_placeholder.png"];

    if (path == nil || [path length] == 0) {
        //default
        [self.imageHeader setImage:placeholder];
        return;
    }

    //load
    NSURL* url = [NSURL URLWithString:path];
    NSURLRequest* request = [NSURLRequest requestWithURL:url];
    __weak CSImageCell* weakComponent = self;
    //
    [self.imageHeader setImageWithURLRequest:request
                               placeholderImage:placeholder
                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                            //
                                            NSLog(@"-OK-\nrequest=%@\nresponse=%@",request,response);
                                            weakComponent.imageHeader.image = image;
                                            [weakComponent setNeedsLayout];
                                            //
                                        }
                                        failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                            //
                                            //TODO: ERror logging
                                            NSLog(@"Failed to load image:\nrequest=%@\nresponse=%@\nerror=%@",request,response,[error description]);
                                            //
                                        }
     ];

}

我在屏幕加载和向左/向右滑动时执行此操作,问题是它停止工作,它加载图像(始终是图像而不是占位符)并且相同的图像仍然存在。日志显示成功和新图像​​的 url,但显示相同。

成功的日志也总是为请求和响应返回 null - 如果这很重要的话。 update: null 并不总是如此,但在后续调用检索相同图像时,它会在图像来自缓存时出现。

这里是我正在使用的图片的链接(文件和位置):

course0_image.jpg

course1_image.jpg

course2_image.jpg

course3_image.jpg

更新

图像在表格的行中,表格布局用于添加通过较大标签调整大小和下推内容的能力。所以第一行是标题图像,然后是标题,然后是日期等等,它始终具有相同数量的单元格,每个单元格出现一次。在滑动时,我正在更改用于填充屏幕的数据。在此更改之前(使用表格)一切都很好,但更长的标题与日期重叠。在更改为表格布局之前,此问题不存在。当我将 UIImageView 放在表格之外时,它会再次开始工作,所以我认为 UITableViewUITableViewCell 与它有关。 p>

更新 2

这里是 sample project (zip)这显示了我面临的问题。

更新 3

问题似乎是在自定义表格单元格中调用 sizeToFit 触发然后调整调用 reloadRowsAtIndexPaths:withRowAnimation: 的表格 View 上的请求,然后 sizeToFit code>tableView:heightForRowAtIndexPath: 执行,一旦此调用被注释掉,图像正在加载。



Best Answer-推荐答案


您需要返回主线程才能修改与 UI 相关的内容。

[self.imageHeader setImageWithURLRequest:request
                            placeholderImage:placeholder
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
                                         //
                                         dispatch_async(dispatch_get_main_queue(), ^{
                                             NSLog(@"-OK-\nrequest=%@\nresponse=%@",request,response);
                                             weakComponent.imageHeader.image = image;
                                             [weakComponent setNeedsLayout];
                                             //
                                         });

                                     }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                         //
                                         //TODO: ERror logging
                                         NSLog(@"Failed to load image:\nrequest=%@\nresponse=%@\nerror=%@",request,response,[error description]);
                                         //
                                     }
     ];

关于ios - setImageWithURLRequest 加载成功,但显示相同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24504344/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4