0

Hi i have high resolution image in local(document). Now i want to list all local images in thumb nail size. I am using UICollectionView to show image while loading resize the image from high resolution to thumbnail. So scrolling speed lack in collectionView. Now i have confusion with how can i cache that resized thumbnail images. which one is the best option to do that. I have used SDWebimages to download and cached images from web. Thank you

 - (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;
}
Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
Madhubalan K
  • 357
  • 5
  • 21
  • Reading from disk and resizing images on the main thread will definitely impact scrolling performance. I would recommend using GCD to do both of these operations in the background and then when finished invoke a method on the main thread to cache them in a `NSDictionary` or `NSArray` for future use. – nrj Feb 20 '14 at 21:15

2 Answers2

1

I recommend storing the images on the file system in app-specific storage and reading those files when necessary. Use the NSFileManager class to accomplish this. Be sure you write code to clean up any files you create so that they don't take up too much space on the user's device.

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];
NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
0

If you are not completely worried about memory consumption you can create an NSDictionary and add your resized image to that. You will need to remove the objects from the dictionary once they are far enough off screen to be reloaded later.

[dictionary setObject:yourSmallImage forKey:indexPath];

Then retrieve it later:

UIImage *image = [dictionary objectForKey:indexPath];
if(!image)
{
    //Load it from your source, no cache found
}
Putz1103
  • 6,211
  • 1
  • 18
  • 25