我想用AVCaptureVideoPreviewLayer作为子图层旋转UIVIew。
在UIView中出现两个按钮(一个将视频预览图层向左旋转,另一个向右旋转),当用户点击其中一个按钮时,每次都会将预览图层旋转1度。
override func viewDidLoad() {
...
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession!)
videoPreviewLayer?.frame = self.view.bounds
videoPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
previewView.layer.addSublayer(videoPreviewLayer!)
}
@objc func rotateRight() {
degrees += 1
// 1st possibility - Scale the UIView that contains the previewLayer for 2x
/*
previewView.transform = CGAffineTransform(scaleX: 2, y: 2)
previewView.transform = CGAffineTransform(rotationAngle: (CGFloat(degrees / 180.0 * .pi)))
previewView.transform = CGAffineTransform.identity
*/
// 2nd possibility - Make bigger the previewLayer so when it's rotate doesn't appear the background color of the UIView
/*
videoPreviewLayer?.frame = CGRect(x: -200, y: -200, width: 800, height: 1200)
videoPreviewLayer?.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
videoPreviewLayer?.transform = CATransform3DMakeRotation(CGFloat(degrees / 180.0 * .pi), 0.0, 0.0, 1.0)
*/
// 3rd possibility
/*
videoPreviewLayer?.frame = CGRect(x: -200, y: -200, width: 800, height: 1200)
videoPreviewLayer?.position = CGPoint(x: view.bounds.width * 0.5, y: view.bounds.width * 0.5)
videoPreviewLayer?.transform = CATransform3DMakeRotation(CGFloat(degrees / 180.0 * .pi), 0.0, 0.0, 1.0)
*/
// 4th possibility
UIView.animate(withDuration: 1, animations: {
self.previewView.contentMode = UIViewContentMode.scaleToFill
self.previewView.transform = CGAffineTransform(rotationAngle: CGFloat(self.degrees))
})
}
需要最终结果 Final result required
实际结果
我尝试了4种不同的方法来创建这种旋转,但所有这些都失败了。 我希望所有这些代码及其结果不会让你更加困惑。
第二个可能性代码的结果 link
第3种可能性代码的结果 link
答案 0 :(得分:0)
你试过吗
if let videoPreviewLayerConnection = videoPreviewLayer.connection {
videoPreviewLayerConnection.videoOrientation = desiredOrientation
}