Reputation: 42139
I have a method in my app that builds UIImages
with specific colors. Since most likely the same colored image will be created multiple times, I would like to cache that UIImage
, then use the cached version rather than building a new one if that specific color is needed.
This is NOT caching of remote images from the web, these are locally created images.
What is the best method to do this? From disk or just save the UIImage
objects into an NSDictionary
? What about NSCache
?
** I would prefer not to have to use library for this. Looking for a simple solution.
Upvotes: 1
Views: 592
Reputation: 119021
It depends how many images you have and how frequently and concurrently each is used.
If you have a set of images which are all used frequently then NSDictionary
is a good choice as it will keep all the images in memory. If you do get a memory warning you can always remove all of the images and then regenerate them when required.
As you're generating the images in code it seems like caching to disk won't be so useful, but that depends on how complex the images are. Again NSDictionary
can be used for an in memory cache, then fail out to disk if nothing in the dict, then recreate if all else fails.
The NSCache
route offers you some multi-threading benefits (if you'd use them) but is generally similar to the NSDictionary
route. You have a little less control as the memory management is handled for you so it's possible that the cache could decide to destroy some of your images more frequently than you might if you manage it explicitly.
In any case you only need a handful of lines on top of your current generation code.
Upvotes: 3