MGImageUtilities is a collection of useful UIImage categories for iPhone and iPad developers, which may be particularly handy if you intend to support the Retina Display on iPhone 4.
I use these in my visual speed-dial app, Favorites for iPhone, and I thought you might find them handy too.
This category lets you resize an arbitrary image to fit into a specified physical size, using one of four resizing methods:
- Scale: scales the image proportionally to fit entirely into the required size, like UIViewContentModeScaleAspectFit.
- Crop: scales the image proportionally to completely fill the required size, cropping towards its center. This is the most useful method, and works like UIViewContentModeScaleAspectFill.
- Start: as for Crop, but crops towards the “start” of the image (the top or left, depending on relative aspect ratios).
- End: as for Crop, but crops towards the “end” of the image (the bottom or right, depending on relative aspect ratios).
This is very useful for caching on-screen-sized versions of images, and generating appropriate images for use on a Retina Display. I use it to appropriately resize photos (and many other things) throughout Favorites:
The category will do the right thing based on the image’s orientation metadata (like in photos taken with the iPhone’s camera, for example), and the scale factor of the device’s main screen (i.e. the resulting image will look as sharp as possible on high-resolution devices like an iPhone 4).
This category takes an image (presumably flat and solid-coloured, like a toolbar icon), and fills its non-transparent pixels with a given colour. You can optionally also specify a fractional opacity at which to composite the original image over the colour-filled region, to give a tinting effect.
This is very useful for generating multiple different-coloured versions of the same image, for example ‘disabled’ or ‘highlighted’ states of the same basic image, without having to make (or update later!) multiple different-coloured bitmap image files. I use it to make the blue and grey versions of the badges in Favorites, for use in the editing UI:
Download the code
You can get MGImageUtilities on github. If you don’t know how to use git, just click the “Download Source” button near the top-right of the github page to download a local copy of the code as a zip or tar archive.
The categories are included in a demo app (for iPhone) which shows how to use them. Here’s a screenshot of the app running:
The license for the code is included with the project; it’s my usual BSD-like license with attribution.
I can’t answer any questions about how to use the code, but I always welcome emails telling me that you’re using it or just saying thanks. If you’re feeling grateful/generous, feel free to take a look at my Amazon Wishlist, or the Paypal Donate link on my Cocoa Source Code page.
Remember that you can always keep up to date with what I’m doing by following me (mattgemmell) on Twitter. I hope you find the code useful!