缓存图像和UICollectionView [英] Caching an Image and UICollectionView
问题描述
我对Objective-C很新,所以希望这一切都有意义。我从服务器下载了图像,并将它们显示在 collectionView cellForItemAtIndexPath:
方法的图像视图中。我面临的问题是图像似乎不是缓存。看起来每次重复使用一个单元时,都会重新下载来自服务器的相关图像。
I'm pretty new to Objective-C so hopefully this all makes sense. I've downloaded images from a server and have displayed them in an image view in the collectionView cellForItemAtIndexPath:
method. The issue I'm facing is that the images don't appear to be caching. It looks like each time a cell is being reused, the associated image from the server is being re-downloaded.
在我的viewDidLoad方法中,我正在创建一个NSMutableDictionary:
In my viewDidLoad method I'm creating a NSMutableDictionary:
imageDictionary = [[NSMutableDictionary alloc]initWithCapacity:50.0];
通过阅读文档并查看类似问题的答案,我认为这个以及下面的代码会足够。我已经在这里工作了几天,我知道有些东西我缺少或者我不理解的概念。
From reading the documentation and looking at answers to similar questions I thought that this, plus the following code would be enough. I've been at this for a couple of days and I know there is something I'm missing or a concept I'm not grasping.
#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;
}
任何帮助都将不胜感激。提前致谢。
Any help would be greatly appreciated. Thanks in advance.
推荐答案
@yuf - 再次感谢方向。 NSCache似乎让我得到了我追求的结果。这是正常工作的代码。如果有人有类似的问题,你可以将以下内容与我原来的问题进行比较。
@yuf - thanks again for the direction. NSCache seems to be getting me the results I was after. Here is the code that is working properly. If anyone has a similar issue, you can compare the following to my original question.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
NSString *imageName = [[self.photos objectAtIndex:indexPath.row] objectForKey:@"fullimage"];
UIImage *image = [imageCache objectForKey:imageName];
if(image){
cell.imageView.image = image;
}
else{
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];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = image;
});
[imageCache setObject:image forKey:imageName];
});
}
return cell;
}
这篇关于缓存图像和UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!