Ignacio Oroná Ignacio Oroná - 6 months ago 44
Objective-C Question

UICollectionView cell image error

I am developing an app with a sample UICollectionView that should load a bunch of icons that it downloads from the internet. So basically the server retrieves a list with the URLs of each icon inside a JSON file, the app parses it, and then each cell downloads the corresponding image.

The problem with this approach seems to be that if the user starts scrolling while the images are downloading, the user will start seeing the images in the wrong order! It's like UICollectionView is trying to 'help me' and it renders the content it's got in the wrong place!

I've seen many threads about this, I tried out most of their suggestions but with out much luck so far. Anybody seen something like this?

enter image description here

This is how the cellForItemAtIndexPath looks like:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PeqImage *image = [self.responseIcons objectAtIndex:indexPath.row];

PeqCustomCell *cell= [self.collectionView dequeueReusableCellWithReuseIdentifier:@"PeqCustomCell" forIndexPath:indexPath];

cell.peqImage = image;
cell.imageName = [NSString stringWithFormat:@"%@.png",image.assetId];

NSString *theImageUrl = [NSString stringWithFormat:@"http://www.XXXX.com/pb/images/uncompressed/%ld.png",(long)indexPath.row];
[cell downloadImage:[NSURL URLWithString:theImageUrl]];

return cell;


And this is how the download algorithm looks like (using UIImageView+AFNetworking):

-(void) downloadImageWithUrl:(NSURL*) url completion:(PeqCustomCellCompletionBlock)completionBlock

{UIImage *placeholder = [UIImage imageNamed:@"placeholder"];
NSURLRequest *jpegURLRequest = [NSURLRequest requestWithURL:url];

self.imageUrl = url.absoluteString;
[self.imageView setImageWithURLRequest:jpegURLRequest
success:^(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image){
image = [self normalizeImage:image];
failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"Error downloading image from network");



You should use SDWebImage library to handle this kind of stuff something like,

    #import <SDWebImage/UIImageView+WebCache.h>


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:MyIdentifier] autorelease];

// Here we use the new provided sd_setImageWithURL: method to load the web image
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

cell.textLabel.text = @"My Text";
return cell;

here is the example of tableview you can use same for your collection view!!

this library cache the images for reusability.

If you want to implement or know native way of this stuff then you can refer Loading Images in UICollectionViewCell: Naive to the Clever.