0

嗨,我在本地(文档)中有高分辨率图像。现在我想以缩略图大小列出所有本地图像。我正在使用 UICollectionView 显示图像,同时加载将图像大小从高分辨率调整为缩略图。所以collectionView缺乏滚动速度。现在我对如何缓存调整大小的缩略图图像感到困惑。哪一个是最好的选择。我使用 SDWebimages 从 Web 下载和缓存图像。谢谢

 - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

    //    NSLog(@"%i path", indexPath.row);
        CollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
        ImageData *imgData =  [self readFromLocal:indexpath.item];
        UIImage *thumb = [self resizeImgToTumb:imgData.image];
        [cell.imgView setimage:thumg];

return cell;
}
4

2 回答 2

1

我建议将文件系统上的图像存储在特定于应用程序的存储中,并在必要时读取这些文件。使用NSFileManager类来完成此操作。确保您编写代码来清理您创建的任何文件,以免它们占用用户设备上的太多空间。

UIImage *image = ...;
NSData *imageData = UIImagePNGRepresentation(image);
NSString *rootDirectory = [NSSearchPathForDirectoriesInDomain(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *imagePath = [rootDirectory stringByAppendingPathComponent:@"filename.png"];
NSError *error;

[imageData writeToFile:imagePath options:NSDataWritingAtomic error:&error];
于 2014-02-20T21:16:44.740 回答
0

如果您不完全担心内存消耗,您可以创建一个 NSDictionary 并将调整大小的图像添加到其中。一旦对象离屏幕足够远以便稍后重新加载,您将需要从字典中删除对象。

[dictionary setObject:yourSmallImage forKey:indexPath];

然后稍后检索它:

UIImage *image = [dictionary objectForKey:indexPath];
if(!image)
{
    //Load it from your source, no cache found
}
于 2014-02-20T21:09:53.997 回答