LoginSignup
6
4

More than 1 year has passed since last update.

AVCaptureVideoPreviewLayerをつかって横向きの動画を撮る方法

Last updated at Posted at 2017-08-03

概要

AVCaptureVideoPreviewLayerをAutoLayoutと一緒に使いながら横向きの動画を撮る際に少しハマったので共有します。

問題点

基本的に、撮る動画の向きに関してはこちらの記事にあるようにAVCaptureOutputのAVCaptureConnectionを変えてやれば問題はありません。

しかし横向きに固定したアプリで動画を撮ろうとするとLayerの中で勝手に動画が回転して、AVCaptureConnectionをLandscapeにしてもPortraitにしても動画が90度回転してしまった状態で表示されてしまいました。

解決方法

解決方法はいたってシンプルでした。先程とりあげた記事にはAVCaptureVideoPreviewLayerが自動で向きを変えてくれるとありますがこれは厳密には違うようで、実際はAVCaptureVideoPreviewLayer自身にsessionとは別のAVCaptureConnectionが設定されていました。

つまり以下のようにするとAVCaptureVideoPreviewLayer内の動画を意図した向きに表示することができます。

`swift
if let videoLayer = AVCaptureVideoPreviewLayer(session: captureSession) {

 videoLayer.frame = previewView.bounds
 videoLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
 videoLayer.connection.videoOrientation = .landscapeRight
 previewView.layer.addSublayer(videoLayer)

}

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