I'm basically after the answer to this question AVCaptureVideoPreviewLayer orientation - need landscape except in Swift.
I'm targeting iOS 8 and AVCaptureVideoPreviewLayer doesn't seem to have a setVideoOrientation function.
How should I be detecting that the orientation has changed, and rotating AVCaptureVideoPreviewLayer appropriately?
Detect and apply it at viewWillLayoutSubviews
or if you're using UIView subclass in layoutSubviews
.
Here's how:
override func viewWillLayoutSubviews() {
let orientation: UIDeviceOrientation = UIDevice.current.orientation
previewLayer.connection?.videoOrientation = {
switch (orientation) {
case .portrait:
return .portrait
case .landscapeRight:
return .landscapeLeft
case .landscapeLeft:
return .landscapeRight
default:
return .portrait
}
}()
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With