短いメモ。
今更気づいたんですが、デバイスの向きとアプリUIの向きは必ずしも一致しないんですね。
たとえばナビゲーションを導入してiPhoneを逆さまにしたとき、NavigationControllerがportraitUpsideDownをサポートしてないせいで、
デバイスの物理的な向き → up side down
アプリの向き → landscape Left のまま
となったりします。
そういうときは UIDevice.current.orientationを使わず、statusBarOrientationを使うと良いです。
avCapture.swift
/* キャプチャ処理での例。 */
/* キャプチャしたCGImageを、現在のアプリの向きを検知して、UIImageを返すところ */
// ステータスバーの向き(アプリの向き)を取得
let interfaceOrientation = UIApplication.shared.statusBarOrientation
// landscape leftの場合
if(interfaceOrientation == UIInterfaceOrientation.landscapeLeft) {
return UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.down)
}
// landscape rightの場合
if(interfaceOrientation == UIInterfaceOrientation.landscapeRight) {
return UIImage(cgImage: imageRef) //そのままUIImageに変換
}
// portrate の場合 (デフォルト)
return UIImage(cgImage: imageRef, scale: 1.0, orientation: UIImageOrientation.right)
Swift4での例が少なかったので共有。