私は、コレクションビューに表示されるたくさんの大きな画像ファイルを持つアプリケーションを構築しています。イメージのサイズのため、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