开发者

How to properly crop an image taken on iPhone 4G (with EXIF rotation data)?

Folks,

I have been unsuccessfully trying to get this code to work with images taken by the camera on an iPhone 4G:

iPhone - CGImageCreateWithImageInRect rotating some camera roll pictures

This code works great for cropping "normal" images - images I have downloaded off of the internet or images that were taken by my iPhone 3G.

iPhone 4G camera photos appear to 开发者_开发技巧be getting cropped in totally random parts of the image after when that rotation code is called.

I even tried using this code: Resizing UIimages pulled from the Camera also ROTATES the UIimage?

And I "resized" the image to the same dimensions it had in order to achieve the rotation, then attempted to crop the image without taking rotation into account. No luck either.

Is there any way I can take an image captured on the iPhone 4G camera and convert it into an image that has the same qualities/properties as an image downloaded off of the internet (ie, has no rotation information and/or its pixels are rotated properly from the outset), so that subsequent operations I attempt to perform (in this case cropping) will work as I expect?

Thanks!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜