Share

engineering

4min read

Implementing a high-performance image manager for iOS

Implementing a high-performance image manager for iOS

One of the most often performed tasks in mobile applications is the on-demand download of images. A ideal solution would deliver images as fast as possible (“under the finger”) and cache aggressively to reduce network usage. Interestingly, Apple only provides very simple (yet performant) base components in the iOS SDK. Leaving the assembly of a working solution to the developer. As our experience shows, it’s not a simple task. We accepted the challenge. The result is our freshly open-sourced PLImageManager which handles all the hard parts of downloading images the right way, including caching, multi- thread download and canceling. Read on, for some of our considerations.

“Single request at a time” queue

A very common solution is to put both the cache query and network download code into a single task (e.g. NSOperation or GCD) and execute them in FIFO order one at a time. The beauty of it lies in it’s simplicity. On the other hand, it’s struggles from problems typical for queue systems.

Lets consider a common real-life situation: a list of posts with thumbnail images. Every time a row gets displayed, a request is ordered for a image. Scrolling quickly, can result in a very long queue. As a result, very long load delays are to be expected. To mitigate, a request-canceling mechanism can be introduced. Images for disappearing rows would no longer take up a place in the queue.

As a side effect of using FIFO, we get request-duplication handling for free. The first request for a image will download and place it in the cache. Following requests will return just after the cache query.

“Multiple request at a time” queue

The “single request at a time” solution degrades with increasing image sizes and slower networks. In both cases this can be traced back to a increase in time the queue gets blocked, waiting for a download to complete. Increasing the number of concurrent tasks seems a viable solution. But concurrent- download doesn’t come without a cost.

Firstly, request-duplication needs a little bit more attention. For example: issuing two requests for the same image, now involves the possibility of both being executed at the same time. As a result both will start a download, and place the result into the cache. Two viable solutions exists: requests can be aggregate into a single tasks, or they can be forced not to operate in parallel. In both cases it will be harder to implement canceling.

Secondly, as our experience shows, performing multiple IO requests to flash at once is not a good idea. Even on background threads. We noticed that it does influence the main/UI thread performance. This is visible for example when scrolling. To gain control of the situation separating the IO load/storage and network access code into smaller subtasks is required. This again introduces more complexity for request-duplication and canceling mechanisms.

All of the above leave a programmer with two rather unpleasant options. To invest a lot of effort into making all concurrent-download, canceling and request-duplication working, or stick to a simpler less performant solution.

PLImageManager

Following the DRY (Don’t Repeat Yourself) approach: the harder a task is to do, the more effort should be taken into doing it only once and properly. And thats exactly what we did at Polidea. PLImageManager source, test and example is now available from our Github account. Like all our open-source components, it’s under a BSD-like license. It has the following characteristics:

  • two level cache - we utilize NSCache for in-memory and flash file storage in temporary directories for caching. This allows the system to handle the nasty low-memory situations for us
  • single callback mechanism - no matter from where the manager gets the image, it will be delivered using a single callback mechanism
  • fast requests - In case the image was stored in memory, the callback will fire synchronously. If not, all the operations will be performed in background tasks, firing the callback asynchronously when ready. In both cases the request call will return almost instantly, ensuring your UI thread doesn’t get blocked
  • anti-duplication mechanism - all requests for a single image happening at the same time will be handled together
  • canceling - a per-request cancel mechanism ensures that as long as there are interested consumers for a image, it will be downloaded. On the other hand, images that were requested, but are no longer needed can be prevented from blocking the queue
  • easy integration and customisability - by externalising use-case specific tasks by means of a provider pattern, PLImageManager can work with the network library of your choice. For convenience, a standard provider using NSUrlRequest is also included

Usage

Setup:

PLURLImageProvider * provider = [PLURLImageProvider new];
PLImageManager * manager = [[PLImageManager alloc] initWithProvider:provider];

Usage:

[manager imageForIdentifier:@”http://placehold.it/350/00aa00/ffffff”
                placeholder:[UIImage imageNamed:@”placeholder”
                   callback:^(UIImage *image, BOOL isPlaceholder) {
    //consume the image here
}];

A more sophisticated example demonstrating the use of canceling is bundled with the project.

PLImageManager on GitHub

Share

Antoni

Principal Software Engineer

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!