我创建了一个 UITableView,并且有一个我需要为其设置图像属性的 URL 数组。我已经设法通过使用 dispatch_async 检索图像来改进表格的滚动功能,并将它们存储在我声明为强属性的 NSCache 中,但是现在当我滚动表格时,我可以看到来自出现的单元格中的前几行,然后当滚动停止时,图像会更新到它们应有的状态,但有时它们也会沿途循环浏览其他一些图像。这是我在 cellForRowAtIndexPathMethod 中的代码,有人知道为什么会这样吗?
UIImage* image = [_imageCache objectForKey:article.imageURL];
if(image){
[cell.imageRight setImage:image];
} else {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0ul);
dispatch_async(queue, ^{
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:article.imageURL]]];
dispatch_async(dispatch_get_main_queue(), ^{
[_imageCache setObject:image forKey:article.imageURL];
[cell.imageRight setImage:img];
[cell setNeedsLayout];
});
});
}