【问题标题】:Using multi GCD to load image in TableView在 TableView 中使用多 GCD 加载图像
【发布时间】: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


【解决方案1】:

问题不在于使用 GCD,问题在于细胞不能那样工作。您有一个包含很多行的表格视图。拥有 tableview 行很便宜,单元格并不便宜。因此,iOS 会一次又一次地重复使用相同的单元格。它只需要你在屏幕上看到的尽可能多的单元格,而不是你拥有的行数。

因此,在您的异步代码中,不要尝试加载单元格并设置图像。相反,将图像存储在数据模型中,并告诉 tableview 重新绘制已更改的 tableview 行。

【讨论】:

  • 您的意思是,所有图像都将使用 GCD 加载并保存在数据模型中。加载完这个数据模型中的所有图片后,我们重绘tableview?
  • 我认为你错了,因为我刚刚评论了加载“image2”的 GCD。并且 tableview 工作正常!!
猜你喜欢
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
相关资源
最近更新 更多