views:

8114

answers:

5
+5  Q: 

Cropping a UIImage

I've got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage and return a small, square representation of an image, similar to what's seen in the album view of the Photos app. (I know I could use a UIImageView and adjust the crop mode to achieve the same results, but these images are sometimes displayed in UIWebViews).

I've started to notice some crashes in this code and I'm a bit stumped. I've got two different theories and I'm wondering if either is on-base.

Theory 1) I achieve the cropping by drawing into an offscreen image context of my target size. Since I want the center portion of the image, I set the CGRect argument passed to drawInRect to something that's larger than the bounds of my image context. I was hoping this was Kosher, but am I instead attempting to draw over other memory that I shouldn't be touching?

Theory 2) I'm doing all of this in a background thread. I know there are portions of UIKit that are restricted to the main thread. I was assuming / hoping that drawing to an offscreen view wasn't one of these. Am I wrong?

(Oh, how I miss NSImage's drawInRect:fromRect:operation:fraction: method.)

+4  A: 

Have you tried using a clipping path to define the crop region?

Giao
A: 

If you're trying to diagnose a crash, you should be running the app under the debugger and making note of what happens when it crashes. You haven't even identified if there is an exception being thrown or you're getting EXC_BAD_ACCESS due to a dangling pointer. Once you know that, then you can start making theories.

benzado
Fair enough. I haven't repro'd this under the debugger, though I do have EXC_BAD_ACCESS messages in the crash log. When I posted this, I was working under the assumption that I'd made a stupid mistake in my implementation and somebody would jump on it (like forgetting a clipping path).
Jablair
A: 

1 is your problem. You need to set a clipping path.

Andrew Grant
+1  A: 

I found myself having to accomplish this for a game I'm working on. Given the lack of information on how to go about doing this I wrote up a quick howto with a code sample. You can find it here: http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/

Lounges
This doesn't work for me - possibly something with image orientation. The simpler code that @HitScan posted works beautifully though.
pix0r
+15  A: 

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);
HitScan
When the `imageOrientation` is set to anything but up, the `CGImage` is rotated with respect to the `UIImage` and the cropping rectangle will be wrong. You can rotate the cropping rectangle accordingly, but the freshly created `UIImage` will have `imageOrientation` up, so that the cropped `CGImage` will be rotated inside it.
zoul