11
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

UIDeviceOrientationを基準にしたUIImageOrientationとAVCaptureVideoOrientationの向き対応

AvFoundationなんかで遊んでいる時にデバイスの向きとカメラの向きで頭がこんがらがります。
背面カメラはそのまま、前面カメラはミラー前提です。

UIImageOrientation


- (UIImageOrientation)currentImageOrientation {
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    // 背面カメラチェック
    BOOL IsBack = [[_activeVideoInput device] position] ==AVCaptureDevicePositionBack;

    switch (deviceOrientation) {
        case UIDeviceOrientationLandscapeLeft:
            return IsBack ?  UIImageOrientationUp:UIImageOrientationDownMirrored;
            break;
        case UIDeviceOrientationLandscapeRight:
            return IsBack? UIImageOrientationDown :UIImageOrientationUpMirrored;
            break;
        case UIDeviceOrientationPortrait:
            return IsBack ?  UIImageOrientationRight:UIImageOrientationLeftMirrored;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            return IsBack ? UIImageOrientationLeft : UIImageOrientationRightMirrored;
            break;
        default:
            return UIImageOrientationRight;
            break;
    }
}

AVCaptureVideoOrientation

- (AVCaptureVideoOrientation)currentVideoOrientation {
    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];

    switch (deviceOrientation) {
        case UIDeviceOrientationLandscapeLeft:
            return AVCaptureVideoOrientationLandscapeRight;
            break;
        case UIDeviceOrientationLandscapeRight:
            return AVCaptureVideoOrientationLandscapeLeft;
            break;
        case UIDeviceOrientationPortrait:
            return AVCaptureVideoOrientationPortrait;
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            return AVCaptureVideoOrientationPortraitUpsideDown;
            break;
        default:
            return AVCaptureVideoOrientationLandscapeRight;
            break;
    }
}

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
11
Help us understand the problem. What are the problem?