获得实际的设备方向

时间:2012-12-18 12:06:55

标签: objective-c ios uiinterfaceorientation

在ios应用程序上工作,即使应用程序以纵向模式修复,我也想弄清楚如何获得实际的设备方向。目前所有的方向检查都给了我肖像。这就是我检查的内容。

CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIInterfaceOrientation orientation2 = [[UIApplication sharedApplication] statusBarOrientation];
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];

基本上我想知道拍摄照片的方向,但如果相机选取器是纵向加载而手机转为横向,则照片的结果方向是纵向。我希望能够在拍摄照片时检查实际的设备方向并相应地进行转换。 关于如何处理这个的任何想法?

编辑:我将考虑使用陀螺仪,这将报告如何。

编辑:抱歉,我应该提到手机本身处于固定的纵向方向。但有没有办法解决这个问题,看看它实际上是什么

2 个答案:

答案 0 :(得分:0)

将拍摄的图像传递给此功能&它会返回您捕获它的方向的图像。

- (UIImage *)fixOrientation:(UIImage *)myImage 
{

    // No-op if the orientation is already correct
    if (myImage.imageOrientation == UIImageOrientationUp) return myImage;

    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (myImage.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, myImage.size.width, myImage.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, myImage.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, myImage.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (myImage.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, myImage.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, myImage.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }

    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    CGContextRef ctx = CGBitmapContextCreate(NULL, myImage.size.width, myImage.size.height,
                                             CGImageGetBitsPerComponent(myImage.CGImage), 0,
                                             CGImageGetColorSpace(myImage.CGImage),
                                             CGImageGetBitmapInfo(myImage.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (myImage.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            // Grr...
            CGContextDrawImage(ctx, CGRectMake(0,0,myImage.size.height,myImage.size.width), myImage.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,myImage.size.width,myImage.size.height), myImage.CGImage);
            break;
    }

    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}

答案 1 :(得分:0)

使用UIDevice的{​​{1}}属性

orientation

它会返回设备的物理方向。

ref