5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[iOS] デバイスの向きとアプリの向き

Last updated at Posted at 2018-08-28

短いメモ。

今更気づいたんですが、デバイスの向きとアプリUIの向きは必ずしも一致しないんですね。
たとえばナビゲーションを導入してiPhoneを逆さまにしたとき、NavigationControllerportraitUpsideDownをサポートしてないせいで、

デバイスの物理的な向き → 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での例が少なかったので共有。

5
4
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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?