我环顾了SO和网页,并没有找到具体的答案。使用块在表格中加载图像 - dispatch_async返回值
很简单,我有一个表格从Flickr加载图片信息。我想在每个单元格的左侧显示图像的缩略图。
为了做到这一点,而不会阻塞主(UI)线程,我使用的块:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"top50places";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//getting the selected row image
NSDictionary* currentImageDictionary=[self.topfifty objectAtIndex:indexPath.row];//topFifty is an array of image dictionaries
//creating the download queue
dispatch_queue_t downloadQueue=dispatch_queue_create("thumbnailImage", NULL);
dispatch_async(downloadQueue, ^{
UIImage *downloadedThumbImage=[self getImage:currentImageDictionary] ;
//Need to go back to the main thread since this is UI related
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = downloadedThumbImage ;
});
});
dispatch_release(downloadQueue);
return cell;
}
现在,这是行不通的。因为返回单元格可能会在执行块之前发生。但是与此同时,我不能返回主队列块中的单元格,因为该块不接受返回参数。
我想避免创建一个UITableViewCell子类。
使用块的任何简单的答案?
感谢 九巴
为什么解决方案未被接受?我很确定它的工作原理。 – esh 2013-06-20 04:34:44