缓存图像和UICollectionView

Bri*_*anS 7 xcode caching image ios uicollectionview

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

在我的viewDidLoad方法中,我正在创建一个NSMutableDictionary:

imageDictionary = [[NSMutableDictionary alloc]initWithCapacity:50.0];
Run Code Online (Sandbox Code Playgroud)

从阅读文档和查看类似问题的答案,我认为这一点,加上以下代码就足够了.我已经在这里工作了几天,我知道有一些我缺少的东西或一个我不理解的概念.

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

Bri*_*anS 15

@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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这不完全正确,因为如果正确地重用单元格,则可以在加载图像和更新单元格时,该单元格的实例用于表示模型中的另一条记录.最好更新缓存,然后调用集合视图reloadItemsAtIndexPaths:并获取cellForItemAtIndexPath以从缓存中设置图像(如果它是可见的). (5认同)