私はObjective-Cをかなり新しくしています。うまくいけば、これはすべて意味があります。私はサーバーから画像をダウンロードし、collectionView cellForItemAtIndexPath:
メソッドの画像ビューで表示しました。私が直面している問題は、画像がキャッシュされていないように見えることです。セルが再利用されるたびに、サーバからの関連イメージが再ダウンロードされているように見えます。イメージとUICollectionViewのキャッシュ
:
imageDictionary = [[NSMutableDictionary alloc]initWithCapacity:50.0];
のドキュメントを読むと、私はこれに加えて、以下のコードは十分だろうと思った同様の質問を見てから。私は数日間この状態にいました。私が紛失しているか、私が把握していない概念があることを知っています。
#pragma mark - UICollectionView Data Source
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;{
NSLog(@"Begin retrieving photos");
return [self.photos count];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = nil;
if (cell.imageView.image == nil) {
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.photos objectAtIndex:indexPath.row] objectForKey:@"fullimage"]]];
UIImage *image = [UIImage imageWithData:data];
[imageDictionary setObject:image forKey:@"Image"];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [imageDictionary objectForKey:@"Image"];
[cell setNeedsDisplay];
});
});
}
return cell;
}
ご協力いただければ幸いです。前もって感謝します。
毎回イメージをダウンロードしているようです。 collectionviewはイメージをキャッシュしません。キャッシングのために独自の辞書を作成する必要があります。 NSCacheを見てください。 – yuf
@yufする - ありがとう! – BrianS