3

Is there any way to detect what orientation a phone was in when an image was taken?

I have a UIImageView on a UIView and I am using an UIImagePicker to take a photo or select one from the camera roll. But if the image has been taken in landscape mode, I want to detect this and resize the imageview to stop the image from being stretched and looking stupid.

Does anyone know if this is possible (I assume so, because the Photos app does this) and if so, how would I go about doing it in code/interface builder settings?

3 Answers 3

8

You can use myImage.imageOrientation, which will give you the UIImageOrientation,

Or if for some reason you want the EXIF information directly in the "imagePickerController:didFinishPickingMediaWithInfo:" method.

by accessing the info dictionary [info objectForKey:@"Orientation"];

Note: This will not relate to UIImageOrientation directly the correlation is as follows.

- (UIImageOrientation)orientationFromEXIF:(int)exif
{  
    UIImageOrientation newOrientation;  
    switch (exif)
    {  
    case 1:  
        newOrientation = UIImageOrientationUp;  
        break;  
    case 3:  
        newOrientation = UIImageOrientationDown;  
        break;  
    case 8:  
        newOrientation = UIImageOrientationLeft;  
        break;  
    case 6:  
        newOrientation = UIImageOrientationRight;  
        break;  
    case 2:  
        newOrientation = UIImageOrientationUpMirrored;  
        break;  
    case 4:  
        newOrientation = UIImageOrientationDownMirrored;  
        break;  
    case 5:  
        newOrientation = UIImageOrientationLeftMirrored;  
        break;  
    case 7:  
        newOrientation = UIImageOrientationRightMirrored;  
        break;  
    }  
    return newOrientation;  
}

But you're better off using .imageOrientation

1
  • 3
    XCode always gives me a "bad access" error when I try to access imageOrientation
    – zakdances
    Commented Jun 8, 2012 at 19:18
2

Check this code in Swift -

class func DetectOrientation(img : UIImage) -> UIImageOrientation{
            var newOrientation = UIImageOrientation.Up
            switch (img.imageOrientation)
            {
            case .Up:
                newOrientation = UIImageOrientation.Up;
                break;
            case .Down:
                newOrientation = UIImageOrientation.Down;
                break;
            case .Left:
                newOrientation = UIImageOrientation.Left;
                break;
            case .Right:
                newOrientation = UIImageOrientation.Right;
                break;
            case .UpMirrored:
                newOrientation = UIImageOrientation.UpMirrored;
                break;
            case .DownMirrored:
                newOrientation = UIImageOrientation.DownMirrored;
                break;
            case .LeftMirrored:
                newOrientation = UIImageOrientation.LeftMirrored;
                break;
            case .RightMirrored:
                newOrientation = UIImageOrientation.RightMirrored;
                break;
            }
            return newOrientation;
        }
0
1

Check out @property(nonatomic, readonly) UIImageOrientation imageOrientation in UIImage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.