Best way to cache images on ios app?

Caching just means keeping a copy of the data that you need so that you don’t have to load it from some slower source. For example, microprocessors often have cache memory where they keep copies of data so that they don’t have to access RAM, which is a lot slower. Hard disks often have memory caches from which the file system can get much quicker access to blocks of data that have been accessed recently.

Similarly, if your app loads a lot of images from the network, it may be in your interest to cache them on your device instead of downloading them every time you need them. There are lots of ways to do that — it sounds like you already found one. You might want to store the images you download in your app’s /Library/Caches directory, especially if you don’t expect them to change. Loading the images from secondary storage will be much, much quicker than loading them over the network.

You might also be interested in the little-known NSCache class for keeping the images you need in memory. NSCache works like a dictionary, but when memory gets tight it’ll start releasing some of its contents. You can check the cache for a given image first, and if you don’t find it there you can then look in your caches directory, and if you don’t find it there you can download it. None of this will speed up image loading on your app the first time you run it, but once your app has downloaded most of what it needs it’ll be much more responsive.

Leave a Comment