I am using SDWebImage for fetching images from server to my table view app in IOS. But the problem is that when I scroll down in table view instead of waiting for the images to load it put the images downloaded in the first few rows of table view and repeat those images till the end row and when it downloads the images it changes those repeated images to the actual image for that row.
NSURL * url = [NSURL URLWithString:string];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadImageWithURL:url
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished,NSURL * url)
{
if (finished && image )
{
NSArray *visibleIndexPaths = [tableView indexPathsForVisibleRows];
if ([visibleIndexPaths containsObject:indexPath]) {
cell.myImage.image = image;
}
}
}];
Actually, it is not a bug with
SDWebImage
, but rather it's the nature of howUITableView
works.downloadImageWithURL
, is an async process,so when your tableView delegate/datasource methods are called, the image isn't downloaded yet, thereforecellForRow
doesn't have an image to display. To overcome this issue you should first check image from cache asif yes then set image to UIImageView otherwise use
downloadImageWithURL
to download image and add cell tag(To display image to correct row) ason successfull download first check correct row as
and set image to UIImageView.Here is setImage method.
And define
showProgressView
andhideProgressView
methods asfinally call
setImage
fromcellForRowAtIndexPath
method(before returning cell) as