3

Can anyone tell me how to use the UIImagePickerController (camera and album) delegates determine the image's orientation, rotate accordingly, and assign it to a UIImageView?

Bryan
  • 17,201
  • 24
  • 97
  • 123

3 Answers3

14

Vodkhang's answer is partially incorrect - modern cameras put an explicit orientation into the image when they take the photo. This has been common for about 5+ years now.

I've previously used the code in this answer to do the rotation by reading the "orientation" info direct from the picture:

UIImagePickerController camera preview is portrait in landscape app

Community
  • 1
  • 1
Adam
  • 32,900
  • 16
  • 126
  • 153
2
    // Use this UIImagePickerController's  delegate method
    - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
        // Getting image user just has shoot
        UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];

        // Fixing to stick with only one orientation (UIImageOrientationUp in this case)
        switch (image.imageOrientation) {
            case UIImageOrientationDown:
            case UIImageOrientationDownMirrored:
            case UIImageOrientationLeft:
            case UIImageOrientationLeftMirrored:
            case UIImageOrientationRight:
            case UIImageOrientationRightMirrored:
                image = [UIImage imageWithCGImage:image.CGImage
                                                  scale:image.scale
                                            orientation:UIImageOrientationUp]; // change this if you need another orientation
                break;
            case UIImageOrientationUp:
            case UIImageOrientationUpMirrored:
                // The image is already in correct orientation
                break;
        }

        // Do whatever you want with image (likely pass it to some UIImageView to display)
        UIImageView *imgView = [[UIImageView alloc] initWithImage:image];

        // Dismiss UIImagePickerController
        [picker dismissModalViewControllerAnimated:NO];
    }

Note: UIImageView reads the imageOrientation property and displays UIImage accordingly.

Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57
  • My app is iPad, landscape only, iOS 8/9. There's a lot of iOS 7-era questions about enabling landscape on `UIImagePickerController` (which for me was working out of the box), but no one addressed why the photos I take while on **landscape right** display upside-down on an UIImageView, while the ones I take in **landscape left** don't. Thank you for your answer. – Nicolas Miari Oct 19 '15 at 10:44
-4

No, UIImagePickerController cannot do it explicitly.

If you want to determine based on image's content. It is kind of hard problem. How can you determine an image's orientation without understanding the content inside of it?

I can suggest you a trick that you examine the width and the length of the returned image and see if it is portrait or landscape. For rotating the image, there are some libraries and codes outside but I didn't try them much. You can take a look at this blog

vodkhang
  • 18,639
  • 11
  • 76
  • 110
  • Thanks for the link. How would I obtain height and width of the image? – Bryan Sep 16 '10 at 17:23
  • After reading your link, I might not need the orientation because the resizing methods on the provided in the link will preserve orientation info and a UIImageView can compensate for orientation automatically. – Bryan Sep 16 '10 at 17:26