I have tried many ways of Asynchronously loading images and they ALL have this problem, this includes 3rd party frameworks. This leads me to believe its something to do with my cell for row index method.
Anyway i am currently loading images Asynchronously using Grand Central Dispatch and Blocks, here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CustomTableCell";
static NSString *CellNib = @"CustomizedCell";
CustomizedCell *cell = (CustomizedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
cell = (CustomizedCell *)[nib objectAtIndex:0];
}
// Configure the cell...
cell.Label.text = [self.TitleArray objectAtIndex:indexPath.row];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *String = [[self.Array objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:String];
NSData *Data = [NSData dataWithContentsOfURL:url];
UIImage *Image = [UIImage imageWithData:Data];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.ImageView.image = Image;
});
});
This seems to load the images Asychrnously, but like with all my attempts at using 3rd party libarys, different code etc, the images flicker. What i mean by this that when you scroll the images of a cell seem to flicker and change to another image of another cell before quickly changing back, making it appear as if it is flickering. This only happens when you scroll and usually only to the top two and bottom two cells. Any ideas why? if anyone could help me with this problem i would really appreciate it!
Thanks!
Well, never use GCD for the moment but I see a problem with your code.
Do you cancel the current process at any moment? I mean, you scroll to view others cell, but there is already a process downloading the image for the (old and reusabled) cell, no? Maybe that's your "flicker" problem.
If I were you, I would create a subclass of UIImageView and implement a way to cancel the current connection if another image wants to load.
By the way, you can use a NSOperation & NSOperationQueue which doesn't use any third-party library.