LoginSignup
6
6

More than 5 years have passed since last update.

iPhoneを横画面にした状態でキャプチャしようとして詰んだ話

Last updated at Posted at 2016-10-09

今回も自分用
たぶんこれでなんとかなる

やりたいこと

横画面にした時、カメラのViewの隣にTextViewを置きたい

IMG_0966.PNG

スクショなのでちょっとわかりにくい
無事完成した

参考にした記事

wift - iOSでビデオカメラを使用時、端末の向きに対応
http://qiita.com/flowtumn/items/9ad3c5554e5ade62a4eb

[Swift]AVFoundationでカメラを使う
http://qiita.com/ShinokiRyosei/items/f70c83a3ab4d22d00ebc

詰んだ箇所

横画面にした時レイアウトがガバったIMG_0959.PNG

原因

画面回転時にAVCaptureVideoPreviewLayerのサイズを横画面時のUIViewのサイズに合わせてなかった

普通に考えて設定し直さないとダメだよな…

解決策

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

        coordinator.animate(
            alongsideTransition: nil,
            completion: {(UIViewControllerTransitionCoordinatorContext) in
                //画面の回転後に向きを教える。
                if let orientation = self.convertUIOrientation2VideoOrientation(f: {return self.appOrientation()}) {
                    self.myVideoLayer.connection.videoOrientation = orientation
                    //ビューのサイズを再設定
                    self.myVideoLayer.frame = self.myCameraView.frame
                }
            }
        )
    }

6
6
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
6
6