Cropping an UIImage

Update 2014-05-28: I wrote this when iOS 3 or so was the hot new thing, I’m certain there are better ways to do this by now, possibly built-in. As many people have mentioned, this method doesn’t take rotation into account; read some additional answers and spread some upvote love around to keep the responses to this question helpful for everyone.

Original response:

I’m going to copy/paste my response to the same question elsewhere:

There isn’t a simple class method to do this, but there is a function that you can use to get the desired results: CGImageCreateWithImageInRect(CGImageRef, CGRect) will help you out.

Here’s a short example using it:

CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef);

Leave a Comment