search
LoginSignup
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;
    }
}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
11
Help us understand the problem. What are the problem?