setImageWithURLRequest loads with success but the same image is displayed

I have a cell (row) in table view that has an image, whenever I need to change the image I'm calling:

-(void)loadThumbnailWithPath:(NSString *)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]);
                                            //
                                        }
     ];

}

I do it when screen loads and on swipe left/right, the problem is that it stops working, it loads an image (always an image not placeholder) and the same image stays. The logs displays success and url of new image but the same is displayed.

the Log for success also always returns null for request and response - if that matters. update: null is not always but on subsequent calls to retrieve the same image, it appears when image comes from cache.

Here are the links to the images I'm using (file and location):

course0_image.jpg

course1_image.jpg

course2_image.jpg

course3_image.jpg

UPDATE

The image is in the row of the table, the table layout was used to add ability to resize and push down content by larger labels. So first row is the header image, then title then date and so on, it has always the same number of cells and each cell appears once. On swipe I'm changing data used to populate screen. Before this change (to use table) all was fine but longer title was overlapping the date. This issue was not there before changing to the table layout. When I placed the UIImageView outside of table it starts to work again so I presume the UITableView or UITableViewCell has something to do with it.

UPDATE 2

Here is the sample project (zip) that shows the issue I'm facing.

UPDATE 3

The issue appears to be with call to sizeToFit in the custom table cell that triggers then resize request on table view which calls reloadRowsAtIndexPaths:withRowAnimation: and after that the tableView:heightForRowAtIndexPath: executes, once this call is commented out the images are loading.

Upvotes: 2

Views: 469

Answers (1)

Thanh-Nhon Nguyen
Thanh-Nhon Nguyen

Reputation: 3418

You need to get back to main thread in order to modify something that related to 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]);
                                         //
                                     }
     ];

Upvotes: 1

Related Questions