LoginSignup
33
30

More than 5 years have passed since last update.

Swift - iOSでビデオカメラを使用時、端末の向きに対応

Last updated at Posted at 2016-01-27

iOSでのビデオカメラ操作

QRコードの読み取る処理の実装が必要になり
下記URLを参考に実装していましたが
http://www.appcoda.com/qr-code-reader-swift/

端末を横向きの状態で、ビデオカメラを使用すると
端末の動きにあったプレビューがされず・・・

ほぼ一日調査してしまったので、忘れないようメモメモ。

解決

resolve.swift
//参考にしたURLを引用しますと、以下を変更することで解決しました。
videoPreviewLayer?.connection.videoOrientation

実装

qr.swift
func appOrientation() -> UIInterfaceOrientation {
    return UIApplication.sharedApplication().statusBarOrientation
}

// UIInterfaceOrientation -> AVCaptureVideoOrientationにConvert
func convertUIOrientation2VideoOrientation(f: () -> UIInterfaceOrientation) -> AVCaptureVideoOrientation? {
    let v = f()
    switch v {
        case UIInterfaceOrientation.Unknown:
            return nil
        default:
            return ([
                UIInterfaceOrientation.Portrait: AVCaptureVideoOrientation.Portrait,
                UIInterfaceOrientation.PortraitUpsideDown: AVCaptureVideoOrientation.PortraitUpsideDown,
                UIInterfaceOrientation.LandscapeLeft: AVCaptureVideoOrientation.LandscapeLeft,
                UIInterfaceOrientation.LandscapeRight: AVCaptureVideoOrientation.LandscapeRight
            ])[v]
    }
}

func initilize() {
    //カメラ周りの初期化など…

    //■■■向きを教える。
    if let orientation = self.convertUIOrientation2VideoOrientation({return self.appOrientation()}) {
        videoPreviewLayer?.connection.videoOrientation = orientation
    }

    view.layer.addSublayer(videoPreviewLayer)
}


//画面の回転にも対応したい時は viewWillTransitionToSize で同じく向きを教える。
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    coordinator.animateAlongsideTransition(
        nil,
        completion: {(UIViewControllerTransitionCoordinatorContext) in
            //画面の回転後に向きを教える。
            if let orientation = self.convertUIOrientation2VideoOrientation({return self.appOrientation()}) {
                videoPreviewLayer?.connection.videoOrientation = orientation
            }
        }
    )
}

iOS8以降で動作を確認しています。
iPadは縦固定で審査が出せないようなので、無事解決して良かった。。

変更履歴

2016/01/27:
 端末の画面回転時、カメラの向きを変えるタイミングをtraitCollectionDidChangeではなく
 viewWillTransitionToSizeに変更しました。

33
30
0

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
  3. You can use dark theme
What you can do with signing up
33
30