【发布时间】:2014-04-20 15:40:07
【问题描述】:
当“cellForRowAtIndexPath”带有回调时,我正在使用异步 GCD 为 UITableView 加载图像。
在我的 tableView 中,每行有 2 个产品(这意味着,在每一行中,我必须使用异步 GCD 加载 2 个图像)-> 如下:
这是我的异步加载图像代码
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Some code to init CustomCell
//Load Label1
//Load Label2
//Load image1
if(product1.thumImgData != nil){
//Update image from datasource
} else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:product1.thumImg]];
if (imgData) {
UIImage *image = [UIImage imageWithData:imgData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
CustomViewProductByCategory *updateCell = (id)[self.tvProduct cellForRowAtIndexPath:indexPath];
if (updateCell){
product1.thumImgData = imgData;
updateCell.imgProduct1.image = image;
[updateCell.indicatorProduct1 stopAnimating];
}
});
}
}
});
}
if(product2.thumImgData != nil){
//Update image from datasource
} else{
//load image2
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:product2.thumImg]];
if (imgData) {
UIImage *image = [UIImage imageWithData:imgData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
CustomViewProductByCategory *updateCell = (id)[self.tvProduct cellForRowAtIndexPath:indexPath];
if (updateCell){
product2.thumImgData = imgData;
updateCell.imgProduct2.image = image;
[updateCell.indicatorProduct2 stopAnimating];
}
});
}
}
});
}
我猜是因为我在 cellForRowAtIndexPath 中使用了 2 个 GCD,所以有时我无法加载一些图像。
有人知道怎么解决吗? 提前谢谢。
【问题讨论】:
-
我有同样的问题,你的图像数据有多大?最好创建一个较小的缩略图图像以在 tableview 单元格中使用。
-
我已请求服务器发送小数据。它只有 80x80 像素
标签: objective-c uitableview grand-central-dispatch