Question

What is the best way to retry a failed image download in cellForItemAtIndexPath? I have a collection view that can show about 20 80x80 thumbnail images. I'm using SDWebImage to download and cache images in the cellForItemAtIndexPath method, but I've found that every so often an image fails to download. I know I can use a completion block and test if the image is nil, but it doesn't seem to be reloading the image correctly (the image still never shows up). Is this how I should be handling reloading the cell? Here's an example of my code.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {   
    NSURL *thumbnailURL = [NSURL URLWithString:url];

    INCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    [cell.imageView setImageWithURL:thumbnailURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
        if (!image) {
            [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
        }
    }];

    return cell;
}
Was it helpful?

Solution

Got an answer on github

You need to use one of the methods with an "options" parameter and specify SDWebImageRetryFailed like so...

[cell.imageView setImageWithURL:thumbnailURL placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageRetryFailed completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {...}];

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top