SundayMonday
SundayMonday

Reputation: 19747

Cropping UIImage from photo library vs iPhone camera

I can crop images from the iPhone's photo library no problem like this:

CGRect topRect = CGRectMake(0, 0, image.size.width, image.size.height / 2);
CGImageRef topCroppedCGImageRef = CGImageCreateWithImageInRect(image.CGImage, 
                                                               topRect);
UIImage *croppedImage = [[UIImage alloc] initWithCGImage:topCroppedCGImageRef];
CGImageRelease(topCroppedCGImageRef);

However this doesn't work when the image comes from the camera. Specifically the cropped image is rotated and the cropped portion isn't as expected. After reading around it sounds like this problem is relatively common. However I've tried the various code fixes and it's not quite working (still have rotation, unexpected cropping and even distortion issues). So I'd like to actually understand why the above cropping doesn't just work for images coming from the camera.

Why doesn't the above cropping method work on images coming from the iPhone's camera?

Upvotes: 1

Views: 810

Answers (1)

deltheil
deltheil

Reputation: 16121

As pointed out by this famous post - Resize a UIImage the right way, this is because you leave out functionality such as EXIF orientation support, an absolute necessity when dealing with photographs taken by the iPhone’s camera.

By default (picture taken in portrait) the image has an EXIF orientation flag = 6 which means the image is rotated 90 degrees counterclockwise:

$ identify -format "%[EXIF:orientation]" myimage.jpg 
6

Upvotes: 2

Related Questions