问题描述
有没有办法检测拍摄图像时手机的方向?
Is there any way to detect what orientation a phone was in when an image was taken?
我在UIView上有一个UIImageView,我正在使用UIImagePicker来拍照或从相机胶卷中选择一张。但是如果图像是以横向模式拍摄的,我想要检测到这一点并调整图像视图的大小以阻止图像被拉伸并看起来很愚蠢。
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?
推荐答案
您可以使用 myImage.imageOrientation
,它将为您提供UIImageOrientation,
You can use myImage.imageOrientation
, which will give you the UIImageOrientation,
或者,如果由于某种原因你想直接在imagePickerController:didFinishPickingMediaWithInfo:方法中获得EXIF信息。
Or if for some reason you want the EXIF information directly in the "imagePickerController:didFinishPickingMediaWithInfo:" method.
访问信息词典 [info objectForKey:@Orientation] ;
注意:这与UIImageOrientation无直接关系相关性如下。
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;
}
但最好不要使用.imageOrientation
But you're better off using .imageOrientation
这篇关于iPhone,如何在拍摄时检测图像的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!