Search code examples
iphoneiosipaduiimageimage-caching

Where to cache remote image files in iOS?


I am planning to cache the images from a server and use show it as a sort slide show in my App. I would be asynchronously loading the images.

I have two options:

  1. Either to cache the images as a File and use it whenever necessary.
  2. Cache the images objects in memory and use it when ever necessary and write it in to files when Application quits.

Which one would be better?

Please let me know if you you have any kind of suggestions regarding caching images.


Solution

  • Your second approach has 2 major flaws:

    1. If there's too many images then your application will get low memory warning and you'll have to dispose your images from memory anyway
    2. It's also not a good idea to save all images to file on application quit - it is not guaranteed that your saving code will finish on application exit (e.g. if it takes too long system may just terminate your app and your images will be lost)

    I'd suggest saving images to files right after you download them and keep in memory reasonable number of images you need to show without visible delay (loading extra images when required and disposing of unnecessary ones)