Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 614
RK- Avatar asked May 12 '11 09:05

RK-


People also ask

Does cache store images?

The bitmap cache stores decoded images as Android Bitmap objects. These are ready for display or postprocessing. On Android 4. x and lower, the bitmap cache's data lives in the ashmem heap, not in the Java heap.

How do you cache an image?

Image caching essentially means downloading an image to the local storage in the app's cache directory (or any other directory that is accessible to the app) and loading it from local storage next time the image loads.

What is image cache in Swift?

Feb 27, 2019. Image caching is an interesting and a very important concept to understand, especially for mobile developers. In many mobile apps, we deal with a lot of images and assets, and we are also required to show all these images to the user as quickly as possible.


2 Answers

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)

like image 101
Vladimir Avatar answered Sep 21 '22 11:09

Vladimir


I would recommend you the first option. Leaves you more flexibility, e.g. when the data size increases the memory size.

like image 21
bertolami Avatar answered Sep 23 '22 11:09

bertolami