2015-11-17 44 views
0

在Objective-C,你可以這樣做:如何在Swift 2中設置previewLayer.connection.videoOrientation?

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

但是在斯威夫特,什麼是這個最後一行翻譯的最好方法?您不能將「deviceOrientation作爲AVCaptureVideoOrientation」。

盡我所能想出迄今:

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

但這似乎不雅。 -1是存在的,因爲它只是在這一天結束的枚舉,而UIDeviceOrientation枚舉有一個「未知」一個在位0的AVCaptureVideoOrientation沒有......

回答

0

試試這個

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

這是我得到的轉換:

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

The converter I used往往不知道什麼代碼應該看起來像一些時間(Int對比CGFloat),所以它可能不是沒有去竊聽工作。讓我知道它是否可以做到你想要的,例如,這個變量可能會變成一個常量。

0
connection.videoOrientation = AVCaptureVideoOrientation.Portrait 

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