xcode 缓存图像和 UICollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13852490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:18:22  来源:igfitidea点击:

Caching an Image and UICollectionView

iosxcodeimagecachinguicollectionview

提问by BrianS

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.

我对 Objective-C 很陌生,所以希望这一切都有意义。我已经从服务器下载了图像,并在collectionView cellForItemAtIndexPath:方法的图像视图中显示了它们。我面临的问题是图像似乎没有缓存。看起来每次重复使用单元格时,都会重新下载来自服务器的相关图像。

In my viewDidLoad method I'm creating a NSMutableDictionary:

在我的 viewDidLoad 方法中,我创建了一个 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.

任何帮助将不胜感激。提前致谢。

回答by BrianS

@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.

@yuf - 再次感谢您的指导。NSCache 似乎让我得到了我想要的结果。这是正常工作的代码。如果有人有类似的问题,您可以将以下内容与我的原始问题进行比较。

- (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;
}