Tip: UIImage Pieces

So you ever wanted to draw just a portion of a UIImage and discovered oh, wait, it’s not actually that easy, and resorted to some messing with scroll views or other such hackery to fake the desired effect quickly? Yeah, us too. But here is a better way:

Drawing Part of a UIImage

On the Mac, NSImage has a handy instance method called drawInRect:fromRect:operation:fraction: that lets you specify exactly which part of an image to draw. On UIImage, we’ve only got the ability to draw the entire image either unless we drop down to Core Graphics calls. We don’t have a nice, easy, convenient way using just UIImage to draw a portion of the image it represents.

I needed this ability in an application I’m working on, so I hacked out the following category…

Just a few lines of code … but tricky lines, if you don’t work at Core Graphics level every day!

Alex | December 8, 2010

Leave a Reply