Resize/Scale of an Image – Take 2 – Coding a Thread Safe Approach

In the first post on image resizing, How to Resize/Scale an Image using an Objective-C Category, I wrote barebones approach to resizing an image. This works well for simple cases, however this approach is not thread safe as it uses the global current context.

I attended a recent Apple Tech Talk and one of the more interesting discussions was on how to create code to dynamically resize images, using an approach that is thread safe. An experienced developer at the event was willing to share an excellent code example that I’ll walk through in this post. The code is a fair amount more complex than the first version I wrote, however, with the complexity comes flexibility.

Building the User Interface

The project to demonstrate thread-safe image resizing is quite simple, essentially a table and a slider, with the later controlling the size of the images in the table. The image below should give you and idea of how things work:

It all starts by building a UITableView and setting the imageView property to one of the images in the application bundle. The code to build (or reload) the table looks as follows:

The relevant line of code is 8, notice the call to requestImageForIndex, the code for this method is shown below. There is a fair amount going on here, first an ImageStateObject is created, this keeps track of the path of the image, a flag indicating if the image has been loaded and a flag indicating if a (resize) operation is in progress. There is also code for managing a queue of operations -to keep focuses on the task at hand, skip down to LINE 20, where we call UImageFromPathScaledToSize passing in the image state object and the preferred size.

Note that when calling the method on line 20, notice the path to the image is passed in as the first parameter.


For completeness, here is the definition of the image state object:

The code for working with the queued operations (queuedOp) we will look into in a future post.


Given a path to an image and a specified size, create a UIImage object and get the scale value based on the current image size and the size to scale to, and finish by creating a new UIImage object from the original image, scaled as requested.

Get Scale Percentage and Create New Image Reference

Below is the code to determine the scale value as a percentage. For example, if the incoming image size (theSize) is 266 x 401 and the destination size (intoSize) is 225 x 225, the returned scale value is .561 (225/401). That is, we want to scale the image to 56% of its current size.

The last step is to create a CGImageRef which will hold bitmap information for the new, scaled image. This reference will then be used to create our final scaled UIImage object.

Project Source Code

You can download the entire project source code here. You’ll find the image processing code shown above is in the source file ImageHelpers.m.

There are many other worthwhile code snippets to look at in this project, including code to create and display gradients (for the table background) as well as working with NSOperation and NSOperationQueue to place resize requests into a queue for processing.

  1. Thank you: that is very nice, helpful code.
    I would suggest though: whenever possible(e.g. when you load the images from your own service/server) you should load them in the final size. The iphone(at least 2g&3g) are pretty slow doing image resize.
    Actually it is slower then requesting thumbnails from a server even on edge.


  2. Superb! Thanks so much for this. I was struggling to maintain performance while generating thumbnails on the main thread.

  3. Hey, I’m having a problem with this, and maybe you can help. I’m trying to resize a normal PNG image, but it gives me this error

    : CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 24 bits/pixel; 3-component color space; kCGImageAlphaNone; 576 bytes/row.

  4. 2 years, and you really save me tonight. Thanks alot!

  5. Wow, thanks. Will try it, no doubt it is what I was looking for! :D

Comments are closed.