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

People,

I tried unsuccessfully to get this code to work with images taken by the camera on the iPhone 4G:

iPhone - CGImageCreateWithImageInRect rotating some camera clips

This code is great for cropping “normal” images — images that I downloaded from the Internet or images that were taken with my iPhone 3G.

Photos of the iPhone 4G camera seem to be cropped in completely random parts of the image after calling this rotation code.

I even tried using this code: Does resizing UI images taken from the camera also rotate the UI image?

And I “resized” the image to the same size as for its rotation, and then tried to crop the image without taking into account the rotation. Bad luck.

Is there a way to capture the image captured on iPhone 4G and convert it to an image that has the same qualities / properties as the image downloaded from the Internet (i.e. does not have rotation information and / or its pixels rotate correctly with from the very beginning), so that the subsequent operations that I try to perform (in this case, cropping) will work, as I expect?

Thanks!

+3
source share
1 answer

There is a lot of sample code out on the Internet for this. All this is incomplete or wrong when it comes to iPhone 4G, because iPhone 4G embeds EXIF ​​data in its JPEG files. This data is displayed through the imageOrientation property, new in iOS 4.

The correct way to crop an area with images with EXIF ​​rotation information is to use this alien code , but instead of using this line

UIImage *resultImage = [[[UIImage alloc] initWithCGImage:resultImageRef] autorelease]; 

You should call it:

 UIImage *resultImage = [[[UIImage alloc] initWithCGImage:resultImageRef scale:1.0 orientation:originalImage.imageOrientation] autorelease]; 

The second call includes rotation information in UIImage from the original image.

Unfortunately, this call is not available in iOS 3.XX; Fortunately, it is unlikely that you will encounter images with EXIF ​​information on these devices because the camera cannot take pictures with rotation information.

+5
source

Source: https://habr.com/ru/post/1390017/


All Articles