ImageCache-bennyguitar 1.0.0

ImageCache-bennyguitar 1.0.0

LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Unclaimed.

  • By
  • BenGordon


A shared cache class that saves UIImages to a Key/Value store and sets UI Elements with the image whenever it's loaded. The class also implements LRU dumping for smart memory management.

Set Up

  • Drag the included ImageCache.{h,m} into your project.
  • Import ImageCache.h into whatever class you will be loading images in.
  • Requires ARC

How to Use

Loading Images

ImageCache is super, super straightforward and easy to use. Right now it handles two cases for setting UIImages: UIImageView and UIButton. You only need two things here, a UIImageView/UIButton and a URL for an image. To lazily load your image URL into the UIImageView/UIButton without blocking the UI thread, your code will look something like this:

// Your UIImageView and UIButton
UIImageView *imgView = [[UIImageView alloc] init];
UIButton *btn = [[UIButton alloc] init];

// Your Image URL
NSURL *url = [NSURL URLWithString:@""];

// Now Load it!
[imgView setImageFromURL:url];
[btn setImageFromURL:url forState:UIControlStateNormal];

And that's it. The ImageCache will check the Cache and pull the UIImage saved in it OR it will go to the web, download the image and set it after it's done downloading.

Dumping the Cache

There are two options provided for getting rid of items in the cache, either by Least Recently Used or dumping the entire thing. The Least Recently Used method takes in an int as a parameter and removes that many items from the cache (up to the total count of the cache). This works by checking the date of access for the images - either loaded from the web or loaded from the cache - and removing the least recently used ones. Both methods can be used like this:

// Removing the 4 most recently used images
[ImageCache dumpLeastRecentlyUsed:4];

// Removing all items from the cache
[ImageCache dumpCache];

Ideally you would call these methods in the didReceiveMemoryWarning method of your ViewController or before entering the background in the App Delegate applicationDidEnterBackground:(UIApplication *)application method.

Design Caveats

So one part of the design process for me was to remove extra-long method names and calls, and the other part was for the primary use of one ImageCache per App. To meet these guidelines, I created the ImageCache to only use class methods that operate on the [ImageCache sharedCache] instance behind the scenes. This means that the function calls end up being something like this [ImageCache dumpCache] instead of [[ImageCache sharedCache] dumpCache], which may be more idiomatic Objective-C. If you would like to use multiple ImageCache classes (who knows, you might just want to) for a single application, I suggest turning those methods into instance methods and changing all mentions of [ImageCache sharedCache] to self. That should free you up to use multiple cache instances in your app.


This repository is licensed under the standard MIT License.