0

在 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 没有...

4

4 回答 4

1

这在swift 4上做得很好:

previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!
于 2018-04-05T10:44:12.833 回答
0

这是我得到的转换:

if UIDeviceOrientationIsPortrait(deviceOrientation) || UIDeviceOrientationIsLandscape(deviceOrientation) {
    var previewLayer: AVCaptureVideoPreviewLayer = previewView.layer
    previewLayer.connection.videoOrientation = deviceOrientation
}

我使用的转换器往往不知道代码应该是什么样子(Intvs. CGFloat),因此如果不进行调试,它可能无法工作。让我知道它是否符合您的要求,例如,变量可能会更改为常量。

于 2015-11-18T14:43:56.163 回答
0

尝试这个

let avLayer = (self.previewView.layer as AVCaptureVideoPreviewLayer)
let connection = avLayer.connection
let orientation = AVCaptureVideoOrientation(rawValue: self.interfaceOrientation.rawValue)!
connection.videoOrientation = orientation
于 2015-11-17T08:05:03.793 回答
0
connection.videoOrientation = AVCaptureVideoOrientation.Portrait

您可以在 swift 头文件中找到常量 AVCaptureVideoOrientation 值。(斯威夫特 2)

于 2016-10-28T20:56:14.753 回答