swift2 如何在Swift 2中设置预览图层.连接.视频方向?

nfg76nw0  于 2022-11-06  发布在  Swift
关注(0)|答案(4)|浏览(189)

在Objective-C中,您可以这样做:

if ( UIDeviceOrientationIsPortrait( deviceOrientation ) || UIDeviceOrientationIsLandscape( deviceOrientation ) ) {
    AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
    previewLayer.connection.videoOrientation = (AVCaptureVideoOrientation)deviceOrientation;
}

但是在Swift中,翻译这最后一行的最好方法是什么呢?你不能把“deviceOrientation作为AVCaptureVideoOrientation”。
目前为止我能想到的最好的办法是:

if(UIDeviceOrientationIsPortrait(deviceOrientation) || UIDeviceOrientationIsLandscape(deviceOrientation)) {
        let myPreviewLayer: AVCaptureVideoPreviewLayer = self.previewLayer
        myPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientation.init(rawValue: deviceOrientation.rawValue-1)!
    }

但是这看起来并不优雅。-1在那里是因为它只是一天结束时的一个枚举,UIDeviceOrientation枚举在位置0有一个“未知”的1,而AVCaptureVideoOrientation没有...

pbpqsu0x

pbpqsu0x1#

这在swift 4上做得很好:

previewLayer.connection?.videoOrientation = AVCaptureVideoOrientation(rawValue: UIApplication.shared.statusBarOrientation.rawValue)!
brqmpdu1

brqmpdu12#

试试这个

let avLayer = (self.previewView.layer as AVCaptureVideoPreviewLayer)
let connection = avLayer.connection
let orientation = AVCaptureVideoOrientation(rawValue: self.interfaceOrientation.rawValue)!
connection.videoOrientation = orientation
iqxoj9l9

iqxoj9l93#

这是我得到的转换:

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

我使用的转换器往往不知道代码应该看起来像一些时候(Int vs. CGFloat),所以它可能不工作,没有调试。让我知道它是否做你想要的,例如,变量可能会被改为常量。

unhi4e5o

unhi4e5o4#

connection.videoOrientation = AVCaptureVideoOrientation.Portrait

您可以在Swift头文件中找到常量AVCaptureVideoOrientation的值。(Swift 2)

相关问题