Let me begin with an easy answer: you should not realize things that have been tested by thousands of people themselves. There are several large libraries that take care of this problem by themselves by implementing disk cache, memory cache, and buffers. Basically everything you ever need, and more.
Two libraries that I can recommend you are as follows:
Both of them are great, so this is actually preferable (I like Haneke better), but they allow you to upload images to different streams, whether from the Internet or from your package or from the file system. They also have extensions for UIImageView that allow you to use the 1-line function to download all images easily and when loading these images they take care of the download.
Cache
For your specific problem, you can use a cache that uses these methods to solve this problem, for example (from the documentation):
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];
Now that you have it in this cache, you can easily download it
SDImageCache *imageCache = [[SDImageCache alloc] initWithNamespace:@"myNamespace"]; [imageCache queryDiskCacheForKey:myCacheKey done:^(UIImage *image) { // image is not nil if image was found }];
All processing and memory balancing is done by the library itself, so you don’t have to worry about anything. You can optionally combine it with resizing methods to store smaller images if they are huge, but it is up to you.
Hope this helps!
Jiri trecak
source share