iPhone, how to detect orientation of image when it

2019-04-11 20:18发布

问题:

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?

回答1:

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



回答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;
        }


回答3:

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