我使用Grand Central Dispatch異步加載UITableViewCell
的圖像。除了在單元格被重用的一些邊界情況下,以及之前的塊加載錯誤的圖像,這種方法效果很好。與GCD重複使用UITableViewCell
我當前的代碼如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
NSString *imagePath = [self imagePathForIndexPath:indexPath];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
[cell setNeedsLayout];
});
});
return cell;
}
據我所知GCD隊列不能停止。那麼如何防止這個邊界案件呢?還是應該使用別的東西而不是GCD來解決這個問題?
我正在使用GMGridView來顯示非常大量的圖像縮略圖,並具有完全相同的問題。我還使用GCD從我的web服務器上即時加載縮略圖。 – Humayun 2012-08-13 09:22:22