我正在從服務器添加圖像。我正在使用NSMutableArray和自定義UITableViewCell。問題:當我運行該項目時。 UITableViewCell顯示相同的圖像。我覺得它讓人耳目一新。我該如何解決這個問題?在Dispatch_queue中的UITableView單元中獲取相同的圖像
下面分派方法我用過,
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//this will start the image loading in bg
dispatch_async(concurrentQueue, ^{
NSError *nserror = nil;
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [imagesArray2 objectAtIndex:indexPath.row]]options:NSDataReadingUncached error:&nserror];
//this will set the image when loading is finished
dispatch_async(dispatch_get_main_queue(), ^{
if (nserror)
{
[cell.imgview setImage:[UIImage imageNamed:@"placeholderimage.png"]];
}
else
{
[cell.imgview setImage:[UIImage imageWithData:imageData]];
}
[mytablview reloadData];
});
});
感謝您的幫助。
爲什麼人們會這樣做?對於所有神聖的愛,請去使用AFNetworking(搜索GitHub)並使用'AFImageView'。它會爲你節省很多時間,麻煩和頭痛等等。 – 2014-11-14 12:42:51
在圖像數據加載之前立即設置您的「佔位符圖像」。 – Astoria 2014-11-14 12:43:07
@GuyKogus它可能是好奇心!或者一些任務要求。人們爲什麼要走直線路?它是編程 - 整個世界! – Astoria 2014-11-14 12:45:32