저는 Objective-C에 익숙하기 때문에이 모든 것이 의미가 있습니다. 서버에서 이미지를 다운로드 한 후 collectionView cellForItemAtIndexPath:
방법으로 이미지보기에 표시했습니다. 내가 직면 한 문제는 이미지가 캐싱되지 않는 것입니다. 셀을 다시 사용할 때마다 서버의 관련 이미지가 다시 다운로드되는 것처럼 보입니다. 설명서를 읽고 난 그이 생각 유사한 질문에 대한 답변보고에서이미지 및 UICollectionView 캐싱
imageDictionary = [[NSMutableDictionary alloc]initWithCapacity:50.0];
을 더한 다음 코드는 충분하다 : 내있는 viewDidLoad 방법에
은 내가 NSMutableDictionary를 만드는거야. 나는 며칠 동안이 일을 해왔으며, 내가 누락 된 것 또는 내가 이해하지 못하는 개념이 있음을 안다.
#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