Sdwebimage: Wrong images loaded in UICollectionViewCell

Created on 23 Nov 2014  路  13Comments  路  Source: SDWebImage/SDWebImage

Hello,

I am using the SDWebImage in an app I am working on and I am happy to say that it works like a charm. I have one problem though. I am using the UIImageView+WebCache category to load thumbnail from URLs on UICollectionViewCells and when I scroll fast upwards and downwards in rare occasions I notice that wrong thumbnails are being loaded on some cells (thumbnails from a different position in the collection view).

Has someone experienced that issue?

Most helpful comment

In your prepareForReuse method you can cancel the image download for the current image. Adding something like [self.moviePoster sd_cancelCurrentImageLoad]; should do the trick. Just setting the UIImageView's image to nil will clear an image if it's currently there, but if there is a background download going on, the image will still get set once the download completes.

All 13 comments

You're experiencing a UICollectionViewCell reuse issue. Nothing to do with SDWebImage, you need to prepare your cells for reuse to ensure images are nulled out before they are shown. Have a look at the documentation for prepareForReuse and define it in your UICollectionViewCell subclass.

Hello, thanks for the fast response. This is not the case. Her's a sample code for one of the cells that I am using.

  • (void)prepareForReuse
    {
    [super prepareForReuse];

[self removeActivityIndicator];
self.moviePoster.image = nil;

self.backgroundColor = [IMSUIAppearanceManager getBackgroundColorStyle01];
self.moviePoster.backgroundColor = [IMSUIAppearanceManager getBackgroundColorStyle01];

self.freeMovieOverlay.hidden = YES;
}

Any other suggestions?

Maybe some kind of wrong usage of the library or something?

@GBoyadziev sounds like a reuse issue to me as well. Are you using one custom cell only?

I am using 3 custom cells, but at the moment of the reproduction of the problem the list is composed only from one type of cell (the one that uses the code I posted).

In your prepareForReuse method you can cancel the image download for the current image. Adding something like [self.moviePoster sd_cancelCurrentImageLoad]; should do the trick. Just setting the UIImageView's image to nil will clear an image if it's currently there, but if there is a background download going on, the image will still get set once the download completes.

Perfect, thank You for the advice!

@GBoyadziev if you need any further assistance on this please reopen

Sorry to reopen this, but i don't quite understand what's causes this issue.
When a cell is reused, it will load another image on its imageView, using sd_setImageXXX method.
Calling sd_setImageXXX will internally call sd_cancelCurrentImageLoad.
So why do i need to cancel the image loading myself ?

Also this issue seems not happen on UITableView. Only UICollectionView.

Simple adding below code helped me.

- (void)prepareForReuse {
    [super prepareForReuse];
    [self.imageView sd_cancelCurrentImageLoad];
}

Thank you @ianb821

This code is awesome....the above one worked for me too.

@KirylBelasheuski Thank you

thanks bensie and KirylBelasheuski,i had solve it

SWIFT 4
Before setting new image to imageView add:

cell.imageView.sd_currentDownloadTask?.cancel()

Was this page helpful?
0 / 5 - 0 ratings