简体   繁体   中英

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. I am using 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.

However, when I hold the phone on the Z axis. It always shows that the UIImage has EXIF = 2 .

I know I can use the accelerometer to tell when the device is on the Z axis. 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 .

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) 在此输入图像描述

EXIF data only reports the XY orientation. There is nothing in the EXIF data that will tell you whether the camera was facing up or down. 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. 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.

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. 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. I have more experience in iOS Apps using Xamarin with C# so forgive me if my ObjectiveC is a little off.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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