Folks,
I have been unsuccessfully trying to get this code to work with images taken by the camera on an iPhone 4G:
iPhone - CGImageCreateWithImageInRect rota
There is a lot of sample code out on the web for this. It's all incomplete or wrong when it comes to iPhone 4G, because iPhone 4G embeds rotation EXIF data into its JPEGs. This data is exposed via the imageOrientation property, new in iOS 4.
The correct way to crop a region with images that have EXIF rotation information is to use this dude's code, but instead of using this line:
UIImage *resultImage = [[[UIImage alloc] initWithCGImage:resultImageRef] autorelease];
You should call this:
UIImage *resultImage = [[[UIImage alloc] initWithCGImage:resultImageRef scale:1.0 orientation:originalImage.imageOrientation] autorelease];
The second call embeds rotation information into the UIImage from the original image.
Unfortunately that call isn't available on iOS 3.XX; fortunately it is unlikely that you will encounter images with EXIF information on those devices because the camera's can't take photos with rotation information.