我正在构建一个应用程序,其中包含许多要在集合视图中显示的大图像文件。由于图像的大小,我发现从它们的URL创建缩略图要快得多,并且使用图像缓存。当我第一次在使用GCD的cellForItemAtIndexPath中实现这个功能时,我发现UI滞后减少很多,但我也注意到当收集视图进入视图并滚动时,单元格中的图像会闪烁并迅速改变。我发现了一些关于类似问题的其他帖子,他们说,检查单元格是否先是零应该解决问题,但不幸的是,这似乎造成另一个问题,其中许多图像从未加载过。有谁知道如何解决这一问题?iOS - 修复cellForItemAtIndexPath图像加载问题
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
ObjectWithPhoto *object = self.objects[indexPath.item];
cell.imageView.image = nil;
NSString *imageName = object.imageName;
NSString *imageKey = [NSString stringWithFormat:@"%@_thumbnail", imageName];
if ([[ImageCache sharedCache] imageForKey:imageKey]) {
cell.imageView.image = [[ImageCache sharedCache] imageForKey:imageKey];
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSURL *imageURL = [[NSBundle mainBundle] URLForResource:imageName withExtension:@"jpg"];
CGSize imageSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.width);
UIImage *thumbnail = [UIImage createThumbnailFromURL:imageURL imageSize:imageSize];
[[ImageCache sharedCache] setImage:thumbnail forKey:imageKey];
dispatch_async(dispatch_get_main_queue(), ^(void) {
PhotoCell *cellToUpdate = (id)[collectionView cellForItemAtIndexPath:indexPath];
if (cellToUpdate) {
cellToUpdate.imageView.image = thumbnail;
} else {
NSLog(@"cell is no long visible");
}
});
});
}
return cell;
}
如果你看我的原始代码,你会注意到,我确实将图像设置为零首先。你的解决方案的其余部分是伟大的,谢谢! – Grambo