2014-02-20 27 views
0

您好我有高分辨率图像在本地(文档)。现在我想列出拇指指甲大小的所有本地图像。我正在使用UICollectionView在加载时将图像从高分辨率调整为缩略图时显示图像。所以滚动速度缺乏collectionView。现在我对如何缓存调整大小的缩略图图像感到困惑。哪一个是做这件事的最佳选择。我已经使用SDWebimages从网上下载和缓存图像。谢谢最好的方法来缓存UIImage不是从网络

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

从磁盘读取数据并在主线程上调整大小的图像肯定会影响滚动性能。我建议使用GCD在后台执行这两个操作,然后在完成时在主线程上调用一个方法以将它们缓存在“NSDictionary”或“NSArray”中以供将来使用。 – nrj

回答

0

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

[dictionary setObject:yourSmallImage forKey:indexPath]; 

然后以后检索:

UIImage *image = [dictionary objectForKey:indexPath]; 
if(!image) 
{ 
    //Load it from your source, no cache found 
} 
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];