UIImage Resizing/Scaling

Shaun, Jan 20

AppKit has an awesmoe convenience method for NSImage to resize methods, setSize.

Over on the iPhone and in UIKit, UIImage's are immutable and the image data can not be directly manipulated.  It would of been nice of Apple to give us a convenience method to return a resized image, but as of the 2.2 firmware they have not.

if you've seen the class-dump generated header files for UIKit, you know there is a private method to resize, however as with all undocumented methods, it can change at any time. More importantly on the iPhone, it violates the SDK agreement and will get your app rejected/pulled on App Store if Apple sees you using it.

With all of that said, I've created a UIImage category that has a scaleToSize: method in it using the CoreGraphics functions, here's the code:

Now you can simply include UIImageResizing.h at the top of your class (or your prefix file so every class has access to it) and do this:

Note: scaleToSize: returns a UIImage set to autorelease, make sure you retain if needed.


blog comments powered by Disqus