views:

40

answers:

1
+1  Q: 

Store cache images

I'm looking for the best read performance for a bunch (~200) cached 80px by 80px images. A large chuck (~50) will all needed to be accessed at once.

Should I store the uiimages (as binary data) in a plist or using core data?

+2  A: 

A couple of basic concepts:

  • CoreData is probably the worst way to go for the image data, the documentation states that BLOB segments in the store cause massive performance problems.
  • Use the file system for what it's built for, read / write access of random chunks of data.

The rest depends on how you organize your data, so here are some thoughts:

  • 80x80 is pretty small, you could probably hold 50 or so in memory at a given time.
  • You need a way to hash the images into some kind of structure so you know which ones to fetch. I would use core data for storing the locations of the images on the file system, and back your view with an NSFetchedResultsController to pull out the list of file names.
  • Use some in memory data structure to store the UIImage objects, a FIFO queue with a size of 50 would work well here, as it gets a new image from the file system it pops out the oldest one.
  • Finally you have to know what images you're going to view and stay ahead of it, file system reads won't be super fast, so you'll need to either chunk your reads or stay far enough of your view to avoid lagging. If your view is showing 50, you might want to keep 100 in memory, 50+ 25 previous and 25 next if you're scrolling for example.

A premature optimization:

  • If read performance is essential, it would be worth while to store the images in "page" sized chunks, such as a zip of 5 or n images that can be read into memory at once, and then split into their corresponding UIImage files.
ImHuntingWabbits
I'd recommend an NSDictionary. Assuming your data model is CoreData, you can store the images in the Dictionary with the pointer value as the key e.g. `[imageThumbnails objectForKey:[NSNumber numberWithInt:(int)referenceObject]]` Wabbits is completely correct about performance concerns - if you don't stay ahead of a tableview and are hitting the file system while they scroll it will be a huge hit. 80x80 thumbs can easily be preloaded in one chunk.
Michael Kernahan
@Michael, an NSDictionary is a good solution is the data is loaded in one chunk, however NSMutableDictionary is not threadsafe and may have issues if you try to limit how many images are actually in memory at a given time.
ImHuntingWabbits
@Wabbits, true enough. In my own solution I actually build up the objects with a mutable dictionary and then store it as a non-mutable. I've found that you can load tons of thumbnails in there without consuming very much memory.
Michael Kernahan