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

前端 未结 1 1114
终归单人心
终归单人心 2021-01-06 17:17

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

1条回答
  •  再見小時候
    2021-01-06 17:34

    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 讨论(0)
提交回复
热议问题