views:

25

answers:

1

What's the best way to save and retrieve an array of images across app restarts? I'm implementing a caching feature for offline viewing of downloaded images and just want to make sure I'm using the right persisting methods.

Thanks!

+1  A: 

The quickest & probably best solution would be to persist your images to disk, no question about it.

You could do something like this to save them as JPEG.

NSData *data = UIImageJPEGRepresentation(image, 1.0f);
[data writeToFile:imagePath atomically:YES];
Yannick Compernol
I'd like to save the entire array of images. Will this still work properly if I swap out the UIImage for an NSArray?
Jeff
You would have to iterate through the array, converting each UIImage to its JPEG representation using something like the code that Yannick provided.
GregInYEG
I undertand that, however, I should mention there will be multiple arrays that need to be cached separately. This way I could later just recreate them when the app launches. If I save the images, this may not work since they will all be save in the same directory and I won't know which set of images belong to which array.
Jeff
Yannick Compernol
I'll take a look at your suggestions and see what works. Many Thanks.
Jeff