In Objective-C you can do this:
if ( UIDeviceOrientationIsPortrait( deviceOrientation ) || UIDeviceOrientationIsLandscape( deviceOrientation ) ) {
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation)deviceOrientation;
}
But in Swift, what's the best way to translate this last line? You can't do "deviceOrientation as AVCaptureVideoOrientation".
Best I could come up with so far is:
if(UIDeviceOrientationIsPortrait(deviceOrientation) || UIDeviceOrientationIsLandscape(deviceOrientation)) {
let myPreviewLayer: AVCaptureVideoPreviewLayer = self.previewLayer
myPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientation.init(rawValue: deviceOrientation.rawValue-1)!
}
But this seems inelegant. The -1 is there because it's just an enum at the end of the day, and the UIDeviceOrientation enum has an "Unknown" one at position 0 that the AVCaptureVideoOrientation does not have...
This is the conversion I got:
The converter I used tends to not know what the code is supposed to look like some times (
Int
vs.CGFloat
) so it might not work without de-bugging. let me know if it does what you want, for example, the variable could probably be changed to a constant.