这里是代码:我正在从背景上的核心数据下载图像并将其放在我的图像视图中。
static NSString *cellIdentifier = @"Pubs Cell";
PubsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
Pub *current = [self.fetchController objectAtIndexPath:indexPath];
cell.name.text = current.name;
cell.description.text = current.descriptionText;
cell.description.backgroundColor = [UIColor clearColor];
cell.description.editable = NO;
dispatch_queue_t queue = dispatch_queue_create("image_queue", NULL);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",current.photo]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pubImage.image = [UIImage imageWithData:data];
});
});
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"light1.jpg"]];
return cell;
任何想法如何解决?提前谢谢。
答案 0 :(得分:5)
在启动异步加载任务之前,您需要将图像设置为nil:
// removing the old image from the reused tablecell
cell.pubImage.image = nil;
// load image asynchronously
dispatch_queue_t queue = dispatch_queue_create("image_queue", NULL);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",current.photo]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.pubImage.image = [UIImage imageWithData:data];
});
});
这是因为加载图像需要一些时间,所以你会看到旧版本,因为tablecells在可用时会重复使用。