xcode iPhone,如何在拍摄时检测图像的方向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5928257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:59:32  来源:igfitidea点击:

iPhone, how to detect orientation of image when it was taken

iphoneobjective-cxcodeinterface-builder

提问by Mark McGookin

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.

我在 UIView 上有一个 UIImageView,我正在使用 UIImagePicker 拍照或从相机胶卷中选择一张。但是如果图像是在横向模式下拍摄的,我想检测到这一点并调整图像视图的大小以阻止图像被拉伸并看起来很愚蠢。

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?

有谁知道这是否可行(我假设是这样,因为照片应用程序会这样做),如果是这样,我将如何在代码/界面构建器设置中进行操作?

回答by averydev

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

你可以使用myImage.imageOrientation,它会给你 UIImageOrientation,

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

或者,如果出于某种原因,您希望直接在“imagePickerController:didFinishPickingMediaWithInfo:”方法中获得 EXIF 信息。

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

通过访问信息字典[info objectForKey:@"Orientation"];

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

注意:这不会直接与 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;  
}

But you're better off using .imageOrientation

但是你最好使用 .imageOrientation

回答by pankaj kshatriya

Check this code in Swift -

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

回答by DavidN

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

查看 UIImage 中的 @property(nonatomic, readonly) UIImageOrientation imageOrientation。