【问题标题】:UITableView thumbnail image performanceUITableView 缩略图图像性能
【发布时间】:2015-02-20 05:48:07
【问题描述】:

我的图像数据正在从核心数据加载到 UITableView 单元格中。这些图像由cellForRowAtIndexPath:indexPath: 中的操作系统(据我所知)自动缩放。毫不奇怪,这会在滚动表格视图时导致很多延迟。

同样,我有一个 UICollectionViewController 将所有相同的图像加载到类似于 iOS Photos 应用程序的集合视图中,并且图像再次以cellForItemAtIndexPath:indexPath 缩放。滚动很慢,但加载 VC 也需要 非常 很长时间。

在这种情况下优化性能的最佳方法是什么?

我做了一些研究,并提出了几个可能的解决方案:

  1. 在viewDidLoad:animated: 中初始化一个“thumbnailArray”,在加载表格/集合视图之前缩放我需要的所有图像,然后使用这个新数组作为视图的数据源。我认为这将解决滚动问题,但不能解决集合视图加载问题。

  2. 在我的图像包装类中为缩略图数据创建新属性。此数据将在创建图像包装对象时创建(即当用户添加图像时)并保存在核心数据中。我认为这比选项#1 更可取。

选项二是最好的方法,还是有我不知道的更好的解决方案?

这是我的cellForRowAtIndexPath:indexPath 和cellForItemAtIndexPath:indexPath: 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CMAEntryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"entriesCell" forIndexPath:indexPath];

    CMAEntry *entry = [self.entries objectAtIndex:indexPath.item];

    cell.speciesLabel.text = [entry.fishSpecies name];
    cell.dateLabel.text = [self.dateFormatter stringFromDate:entry.date];

    if (entry.location)
        cell.locationLabel.text = [entry locationAsString];
    else
        cell.locationLabel.text = @"No Location";

    if ([entry.images count] > 0)
        cell.thumbImage.image = [[entry.images objectAtIndex:0] dataAsUIImage];
    else
        cell.thumbImage.image = [UIImage imageNamed:@"no_image.png"];

    if (indexPath.item % 2 == 0)
        [cell setBackgroundColor:CELL_COLOR_DARK];
    else
        [cell setBackgroundColor:CELL_COLOR_LIGHT];

    return cell;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thumbnailCell" forIndexPath:indexPath];

    UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
    [imageView setImage:[[self.imagesArray objectAtIndex:indexPath.item] dataAsUIImage]];

    return cell;
}

提前致谢!

科恩

【问题讨论】:

    标签: ios objective-c performance core-data uiimage


    【解决方案1】:

    感谢您的意见,伙计们,但这是对我来说非常有效的解决方案。我唯一不喜欢的是,现在我存储了一个完整图像的 NSData 实例,以及核心数据中的两个不同大小的缩略图;不过,这似乎不是问题。

    我所做的是向我的 NSManagedObject 子类添加几个属性来存储缩略图。缩略图数据在用户选择图像时初始化,然后与原始图像一起保存在核心数据中。

    然后,我在需要它们的视图控制器中将缩略图异步加载到集合中。

    效果很好。摆脱我遇到的所有问题。

    【讨论】:

      【解决方案2】:

      1 我认为最好的方法是在后台线程中加载图像,以便快速加载表格视图。

      2 您还可以使用批量大小的 coredata 功能来仅加载必要的数据。

      3 也许在内存中为图像使用某种类型的缓存可能会有所帮助

      【讨论】:

      • 1.但我不希望表格视图显示为空图像。也许它不会被注意到? 2. 核心数据不会自动做这个吗?仅在使用时才加载属性? 3. 如果图像已经作为二进制数据存储在核心数据中,缓存图像会不会很糟糕?我不下载任何图像,数据只是存储在 NSManagedObject 子类中。
      【解决方案3】:

      在我的一个项目中,我遇到了同样的问题,我通过使用AsyncImageView 为我的表格视图单元加载缩略图来解决它。它将异步加载图像。

      @property (nonatomic,strong)AsyncImageView * thumbImageView;
         self.thumbImageView =[[AsyncImageView alloc] init];
      cell.thumbImageView.imageURL =[NSURL fileURLWithPath:UrlString];
      

      【讨论】:

      • 你是在 cellForRowAtIndexPath 还是 viewDidLoad 中初始化 thumbnailView?现在我什至不使用缩略图,我只使用用户选择的那个。
      • 是的,我是在 cellForRowAtIndexPath 中完成的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2012-01-27
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多