UICollectionView and AFNetworking image doesn't load sometimes

207 views Asked by At

I have an app where you can take picture, once it's taken it's automatically uploaded to a server which generate a thumb of this pic. After the photo is taken, my app return to the UICollectionView. The problem is sometimes, the last picture doesn't load the thumb, I only have the placeholder and not the thumb of the actual pic. I need to reload the view or scroll to see it.

Here is my loading function:

    - (void)setCellPhotos:(UIImageView *)avatar avatarUrl:(NSString *)avatarUrl
{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = YES;

    NSURL *url = [NSURL URLWithString:avatarUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    UIImage *img = [UIImage imageNamed:@"square"];
    __weak UIImageView *avatarView = avatar;
    [avatarView setImageWithURLRequest:request
                      placeholderImage:img
                               success:^(NSURLRequest *request, NSHTTPURLResponse *response,
                                         UIImage *image)
     {
         avatarView.image = image;
         UIApplication* app = [UIApplication sharedApplication];
         app.networkActivityIndicatorVisible = NO;

     }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
    {
        UIApplication* app = [UIApplication sharedApplication];
        app.networkActivityIndicatorVisible = NO;
    }];
}

My cell function:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];

    NSDictionary *pic = [photosArray objectAtIndex:indexPath.row];
    UIImageView *picImageView = (UIImageView *)[cell viewWithTag:100];

    [self setCellPhotos:picImageView avatarUrl:[pic objectForKey:@"thumb"]];

    return cell;
}

I also have a delegate which return me when the upload of the pic is completed and then I do a request to get the new gallery followed by a:

[self.collectionView reloadData];

I checked on the server side and the thumb is generated before it send me a response.

Thanks for your help

1

There are 1 answers

2
MDB983 On

Your problem is that the response from the NURLRequest happens on a background thread, and all UI updates need to be on the main thread. If you wait long enough you might find that the picture eventually shows up.

To fix this, simply modify the return block to include setting the image on the main thread such as;

 - (void)setCellPhotos:(UIImageView *)avatar avatarUrl:(NSString *)avatarUrl
 {
     UIApplication* app = [UIApplication sharedApplication];
     app.networkActivityIndicatorVisible = YES;

     NSURL *url = [NSURL URLWithString:avatarUrl];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     UIImage *img = [UIImage imageNamed:@"square"];
     __weak UIImageView *avatarView = avatar;
     [avatarView setImageWithURLRequest:request
                  placeholderImage:img
                           success:^(NSURLRequest *request, NSHTTPURLResponse *response,
                                     UIImage *image)
     {
           dispatch_async(dispatch_get_main_queue(), ^{
             avatarView.image = image;
             UIApplication* app = [UIApplication sharedApplication];
             app.networkActivityIndicatorVisible = NO;
            });

      }failure:^(NSURLRequest *request, NSHTTPURLRes ponse *response, NSError *error)
      {
          UIApplication* app = [UIApplication sharedApplication];
          app.networkActivityIndicatorVisible = NO;
  }];
 }

You should probably also modify the failure block