0

In my tableview, i am downloading images from a web service. I want the default image to be set in the image container while it grabs the images from the web service. The asynchronous downloading using GCD is happening in a separate class so it can lazily load the images. Here is the code in cellForRowAtIndexPath:

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

    ItemsViewCell *cell = (ItemsViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    FeedItem *item = [[channel items] objectAtIndex:[indexPath row]];

    cell.titleLabel.text = [item title];

    if (cell.thumbContainer.image != nil) {
        cell.thumbContainer.image = [item thumbnail];

    }
    else
        cell.thumbContainer.image = [UIImage imageNamed:@"defaultCellImage.png"];

}

The problem that i am facing is that it sets the default image but it keeps on blinking while the images from the web service are loaded. Can any one point out how to fix it?

UPDATE: Here is the method in another class that is downloading the images asynchronously:

- (void) downloadThumbnails:(NSURL *)finalUrl
{
    dispatch_group_async(((RSSChannel *)self.parentParserDelegate).imageDownloadGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSMutableData *tempData = [NSData dataWithContentsOfURL:finalUrl];
        [tempData writeToURL:[self cachedFileURLFromFileName:self.thumbFile] atomically:YES];

        dispatch_async(dispatch_get_main_queue(), ^{

            thumbnail = [UIImage imageWithData:tempData];

             [[NSNotificationCenter defaultCenter] postNotificationName:@"DataSaved" object:nil];

      });
    });
}

I saw some related questions on StackOverflow but they didn't help what exactly the problem could be.

UPDATE: Kindly do not suggest using SDWebImage or any other image handling library.

Thanks

4

1 回答 1

2
if (cell.thumbContainer.image != nil) {
    cell.thumbContainer.image = [item thumbnail];

}
else
    cell.thumbContainer.image = [UIImage imageNamed:@"defaultCellImage.png"];

第一次,它设置默认值,第二次,执行 if stmts。如果图像没有下载,它什么都不设置,第三次,再次设置默认值。

改成这个就好了

if ([item thumbnail] != nil) {
    cell.thumbContainer.image = [item thumbnail];

}
else
    cell.thumbContainer.image = [UIImage imageNamed:@"defaultCellImage.png"];
于 2013-05-09T08:49:47.530 回答