【问题标题】:UIImage Orientation when iPhone is on Z-AxisiPhone在Z轴上时的UIImage方向
【发布时间】:2026-01-19 02:30:01
【问题描述】:

我使用的代码与此博客 http://blog.logichigh.com/2008/06/05/uiimage-fix/ 上的代码相似 在我用 iPhone 相机拍摄图像后旋转图像。我正在使用AVFoundation

我这里已经提取了相关代码:

    case UIImageOrientationUp: //EXIF = 1  
        transform = CGAffineTransformIdentity;  
        break;  

    case UIImageOrientationUpMirrored: //EXIF = 2  
        transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);  
        transform = CGAffineTransformScale(transform, -1.0, 1.0);  
        break;  

    case UIImageOrientationDown: //EXIF = 3  
        transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);  
        transform = CGAffineTransformRotate(transform, M_PI);  
        break;  

    case UIImageOrientationDownMirrored: //EXIF = 4  
        transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);  
        transform = CGAffineTransformScale(transform, 1.0, -1.0);  
        break;  

    case UIImageOrientationLeftMirrored: //EXIF = 5  
        boundHeight = bounds.size.height;  
        bounds.size.height = bounds.size.width;  
        bounds.size.width = boundHeight;  
        transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);  
        transform = CGAffineTransformScale(transform, -1.0, 1.0);  
        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);  
        break;  

    case UIImageOrientationLeft: //EXIF = 6  
        boundHeight = bounds.size.height;  
        bounds.size.height = bounds.size.width;  
        bounds.size.width = boundHeight;  
        transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);  
        transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);  
        break;  

    case UIImageOrientationRightMirrored: //EXIF = 7  
        boundHeight = bounds.size.height;  
        bounds.size.height = bounds.size.width;  
        bounds.size.width = boundHeight;  
        transform = CGAffineTransformMakeScale(-1.0, 1.0);  
        transform = CGAffineTransformRotate(transform, M_PI / 2.0);  
        break;  

    case UIImageOrientationRight: //EXIF = 8  
        boundHeight = bounds.size.height;  
        bounds.size.height = bounds.size.width;  
        bounds.size.width = boundHeight;  
        transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);  
        transform = CGAffineTransformRotate(transform, M_PI / 2.0);  
        break;  

当手机放在XY 轴上时,这可以正常工作。

但是,当我将手机放在Z 轴上时。它总是显示UIImageEXIF = 2

我知道我可以使用加速度计来判断设备何时位于Z 轴上。但是,我无法看到一条路径可以引导我在拍摄时区分图像,并标记此标记,因为它们仍然有EXIF = 2

即它可以让我区分在 Z 上拍摄的照片。但它不允许我区分照片本身,例如Landscape1(iPhone Home 按钮在左侧,Portrait,Landscape2(iPhone Home 按钮在右侧)

【问题讨论】:

  • 你用的是什么版本的ios?
  • 我正在测试的手机有 iOS 6。
  • 请问您需要使用陀螺仪吗?
  • 当我们谈论拍摄照片时相机的方向(即纵向主页底部,纵向主页顶部,横向主页左侧,横向主页右侧)时,Z轴在哪里?
  • 根本无法判断。不管手机朝上还是朝下,谁来拍照?

标签: iphone ios uiimage core-graphics avfoundation


【解决方案1】:

EXIF 数据仅报告 X-Y 方向。 EXIF 数据中没有任何内容可以告诉您相机是朝上还是朝下。您可以在捕获图像时获取设备方向:

[[UIDevice currentDevice] orientation]

然后,您只需将图像及其上/下方向与 EXIF 数据分开跟踪即可。如果您将图像存储在应用程序中,则可以使用简单的数据库表,甚至可以使用图像名称作为键、向上/向下方向作为值的序列化 NSDictionary。

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。我根据设备方向将屏幕上的视图定位在某些位置。但是,当设备平放而没有明显处于横向或纵向时,来自[[UIDevice currentDevice]orientation] 的方向是不可靠的。我最终通过使用[[[UIApplication] sharedApplication] statusBarOrientation] 解决了这个问题,它将始终返回状态栏在屏幕上显示的当前方向。我在使用 Xamarin 和 C# 的 iOS 应用方面有更多经验,所以如果我的 ObjectiveC 有点偏离,请原谅我。

    【讨论】: