Search code examples
uiimagepickercontrolleruiimageorientation

UIImageOrientation/UIImagePickerController


I am using the Camera to take pictures in an IOS app. It works, the only issue I have is when I try to figure out how the user was holding the device when taking the picture.

It would be much better if I could display the images in a proper way, not upside down or rotated 90 degrees.

Looking for a solution leads me to the fact that I should use the imageOrientation of UIImage but in reality:

theImage.imageOrientation is always equal to UIImageOrientationUp

What am I missing?

I did various research and experiment but nothing changes. I am not showing any more code at this point, for the simple reason, I do not know which part to show to help find the problem.


Solution

  • Since I was able to find a solution on my own. I put it here, in case it may be useful to someone else.

    Looking at the value of the "imageOrientation" field on the saved image (like I was doing), indeed for some reason always shows up as UIImageOrientationUp. On the other hand looking at it inside the imagePickerController:didFinishPickingMediaWithInfo: is much more meaningful.

    After some more research and trials, I ended up with a method looking like that:

    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        ……
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        ……
    
        if ((image.imageOrientation==UIImageOrientationLeft)||
            (image.imageOrientation==UIImageOrientationRight)||
            (image.imageOrientation==UIImageOrientationDown)) {
            UIGraphicsBeginImageContext(image.size);
            [image drawAtPoint:CGPointMake(0.0, 0.0)];
            image=UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    
        NSData *mediaData = UIImagePNGRepresentation(image);
        ……
    }
    

    And then my picture shows properly oriented, when I want to use it later in my app.