Search code examples
iosobjective-cuitableviewcache-control

how to use Cache directory for image


I am showing picture in tableview . I want that if image is already downloaded then do not download again .how to implement this scenario using cache directory.


Solution

  • Take a look at NSCache, it works like a mutable dictionary but it thread safe.

    NSCache objects differ from other mutable collections in a few ways:

    The NSCache class incorporates various auto-removal policies, which ensure that it does not use too much of the system’s memory. The system automatically carries out these policies if memory is needed by other applications. When invoked, these policies remove some items from the cache, minimizing its memory footprint.

    You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

    Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.

    These features are necessary for the NSCache class, as the cache may decide to automatically mutate itself asynchronously behind the scenes if it is called to free up memory.

    You can key your images using their URL and the image itself as a value.
    Documentation.
    NSHipster post.