在 Objective-C 中,你可以这样做:
if ( UIDeviceOrientationIsPortrait( deviceOrientation ) || UIDeviceOrientationIsLandscape( deviceOrientation ) ) {
AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation)deviceOrientation;
}
但是在 Swift 中,翻译最后一行的最佳方式是什么?你不能做“deviceOrientation as AVCaptureVideoOrientation”。
到目前为止,我能想到的最好的是:
if(UIDeviceOrientationIsPortrait(deviceOrientation) || UIDeviceOrientationIsLandscape(deviceOrientation)) {
let myPreviewLayer: AVCaptureVideoPreviewLayer = self.previewLayer
myPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientation.init(rawValue: deviceOrientation.rawValue-1)!
}
但这似乎不优雅。-1 在那里,因为它只是一天结束时的枚举,而 UIDeviceOrientation 枚举在位置 0 有一个“未知”,而 AVCaptureVideoOrientation 没有...