【发布时间】:2015-02-20 05:48:07
【问题描述】:
我的图像数据正在从核心数据加载到 UITableView 单元格中。这些图像由cellForRowAtIndexPath:indexPath: 中的操作系统(据我所知)自动缩放。毫不奇怪,这会在滚动表格视图时导致很多延迟。
同样,我有一个 UICollectionViewController 将所有相同的图像加载到类似于 iOS Photos 应用程序的集合视图中,并且图像再次以cellForItemAtIndexPath:indexPath 缩放。滚动很慢,但加载 VC 也需要 非常 很长时间。
在这种情况下优化性能的最佳方法是什么?
我做了一些研究,并提出了几个可能的解决方案:
在
viewDidLoad:animated:中初始化一个“thumbnailArray”,在加载表格/集合视图之前缩放我需要的所有图像,然后使用这个新数组作为视图的数据源。我认为这将解决滚动问题,但不能解决集合视图加载问题。在我的图像包装类中为缩略图数据创建新属性。此数据将在创建图像包装对象时创建(即当用户添加图像时)并保存在核心数据中。我认为这比选项#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