IPhone how to determine the orientation of the image when it was taken

Is there a way to determine what orientation the phone was in when shooting the image?

I have a UIImageView on a UIView, and I use UIImagePicker to take a picture or select one from the camera’s movie. But if the image was taken in landscape mode, I want to detect this and resize the image so that the image does not stretch and look silly.

Does anyone know if this is possible (I think so because the Photos application does this), and if so, how can I do this in the code / interface linker settings?

+3
source share
3 answers

You can use myImage.imageOrientationone that will give you a UIImageOrientation,

- EXIF ​​ imagePickerController: didFinishPickingMediaWithInfo:.

[info objectForKey: @ "" ];

. UIImageOrientation, .

- (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;  
}

.imageOrientation

+8

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;
        }
+2

Check out @property (non-atomic, read-only) UIImageOrientation imageOrientation in UIImage.

+1
source

All Articles