2016-11-16 36 views
2

我有一個相機應用程序,它允許用戶拍照和錄製視頻。 iPhone使用適配器連接到醫用耳鏡,因此捕獲的視頻非常小(大約一角錢)。我需要能夠縮放視頻以填滿屏幕,但一直未能弄清楚如何操作。AVCapture和縮放在Swift中的previewLayer

我發現this answer here on SO使用ObjC,但還沒有成功地將它翻譯成Swift。我非常接近但陷入困境。這是我用於處理UIPinchGestureRecgoznier代碼:

@IBAction func handlePinchGesture(sender: UIPinchGestureRecognizer) { 

    var initialVideoZoomFactor: CGFloat = 0.0 

    if (sender.state == UIGestureRecognizerState.began) { 
     initialVideoZoomFactor = (captureDevice?.videoZoomFactor)! 
    } else { 
     let scale: CGFloat = min(max(1, initialVideoZoomFactor * sender.scale), 4) 

     CATransaction.begin() 
     CATransaction.setAnimationDuration(0.01) 
     previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale) 
     CATransaction.commit() 

     if ((captureDevice?.lockForConfiguration()) != nil) { 
      captureDevice?.videoZoomFactor = scale 
      captureDevice?.unlockForConfiguration() 
     } 

    } 
} 

這條線......

previewLayer?.transform = CGAffineTransform(scaleX: scale, y: scale) 

...給我的錯誤「無法指定類型的值「CGAffineTransform爲鍵入‘CGTransform3D’。我試圖弄清楚這一點,但我試圖解決這個問題一直沒有結果。

回答

2

想通了:改變了問題的行:

previewLayer?.setAffineTransform(CGAffineTransform(scaleX: scale, y: scale))