简体   繁体   English

iPhone在Z轴上时的UIImage方向

[英]UIImage Orientation when iPhone is on Z-Axis

I am using code similar to that found on this blog http://blog.logichigh.com/2008/06/05/uiimage-fix/ to rotate the images after I have taken them with the iPhone camera. 我正在使用类似于此博客http://blog.logichigh.com/2008/06/05/uiimage-fix/上的代码,在我用iPhone相机拍摄后旋转图像。 I am using AVFoundation . 我正在使用AVFoundation

I have extracted the relevant code here: 我在这里提取了相关代码:

    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;  

This works fine when the phone is held on the X or Y axis. 当手机放在X轴或Y轴上时,此功能正常。

However, when I hold the phone on the Z axis. 但是,当我把手机放在Z轴上时。 It always shows that the UIImage has EXIF = 2 . 它总是显示UIImage EXIF = 2

I know I can use the accelerometer to tell when the device is on the Z axis. 我知道我可以使用加速度计来判断设备何时在Z轴上。 However, I am unable to see a path that will lead me to distinguish between the images when taken, with this flagged, as they all still have EXIF = 2 . 但是,我无法看到一条路径,可以让我区分拍摄时的图像,标记为此,因为它们仍然具有EXIF = 2

ie It will allow me to distinguish between photos that were taken on the Z. But it will not allow me to distinguish between the photos themselves eg Landscape1 (iPhone Home button on left, Portrait, Landscape2 (iPhone Home button on right) 即它可以让我区分在Z上拍摄的照片。但它不允许我区分照片本身,例如Landscape1(左侧的iPhone Home按钮,Portrait,Landscape2(右侧的iPhone Home按钮) 在此输入图像描述

EXIF data only reports the XY orientation. EXIF数据仅报告XY方向。 There is nothing in the EXIF data that will tell you whether the camera was facing up or down. EXIF数据中没有任何内容可以告诉您相机是朝上还是朝下。 You can grab the device orientation at the time you capture the image: 您可以在捕获图像时抓取设备方向:

[[UIDevice currentDevice] orientation]

Then you'll just need to keep track of the image and its up/down orientation separate from the EXIF data. 然后,您只需要跟踪图像及其上/下方向,与EXIF数据分开。 If you're storing the images in your app, a simple database table would do, or even a serialized NSDictionary with the image name as key and up/down orientation as value. 如果您将图像存储在应用程序中,则可以使用简单的数据库表,甚至是序列化的NSDictionary,其图像名称为键,上/下方向为值。

I ran into a similar issue. 我遇到了类似的问题。 I was positioning views on the screen in certain positions depending on device orientation. 我根据设备方向在某些位置将屏幕定位在屏幕上。 However, where the device was Laying flat without being obviously in landscape or portrait the orientation from [[UIDevice currentDevice] orientation] was unreliable. 但是,如果设备平放而没有明显的横向或纵向,则[[UIDevice currentDevice]方向]的方向不可靠。 I ended up solving this by using [[[UIApplication] sharedApplication] statusBarOrientation] which will always return the current orientation in which the status bar is being presented on the screen. 我最后通过使用[[[UIApplication] sharedApplication] statusBarOrientation]来解决这个问题,它将始终返回屏幕上显示状态栏的当前方向。 I have more experience in iOS Apps using Xamarin with C# so forgive me if my ObjectiveC is a little off. 我在使用Xamarin和C#的iOS应用程序方面有更多的经验,所以请原谅我,如果我的ObjectiveC有点偏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM