iPhone,如何检测拍摄的图片方向?

3
有没有办法检测手机拍照时的方向?
我在UIView中使用UIImageView,并使用UIImagePicker拍摄或从相机胶卷中选择照片。但如果图片是横向拍摄的,我想检测到这一点并调整图像视图的大小,以防止图像被拉伸和看起来很愚蠢。
有人知道是否可能实现这一点(我认为是可能的,因为照片应用程序可以做到这一点),如果可能,我该如何在代码/界面构建器设置中进行操作?
3个回答

8
您可以使用myImage.imageOrientation,它将给您UIImageOrientation,或者如果出于某种原因您想要直接在“imagePickerController:didFinishPickingMediaWithInfo:”方法中获得EXIF信息,可以通过访问信息字典[info objectForKey:@"Orientation"]来实现;请注意:这与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。

3
每当我尝试访问imageOrientation时,XCode总是会给出“bad access(坏地址)”错误。 - zakdances

2

请查看以下 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;
        }

1

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接