1
我已經創建了一個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];
});
});
}