Converting UIImage to CGImage cause rotation of Image

Kapil Choubisa picture Kapil Choubisa · May 25, 2012 · Viewed 9.9k times · Source

I am trying to crop image by my custom cropper. Not from the UIImagePickerController's. Now for that I am showing the Image captured from Image Picker in a UIImgeView by handling UIImagePicker's delegate:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    imvOriginalImage.image = image;
}

For cropping I have to convert this Image to CGImageRef. The Image is captured in portrait mode. Now when I convert this image in CGImageRef image orientation get changed. Even if I use following:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    //imvOriginalImage.image = image;
    CGImageRef imgRef = [image CGImage];
    UIImage *img = [UIImage imageWithCGImage:imgRef];
    [imvOriginalImage setImage:img];

}

Image Orientation get changed. This happens only for those Image which are captured in portrait mode. Landscape images not have this issue.

  1. Why this is happening?
  2. How to solve this?

Thanks In Advance.

Answer

Saad picture Saad · May 25, 2012

This is that iPhone camera rotates the image comparing with Home button. It uses EXIF that is meta data of image. See this thread:

iPhone image rotation