2017-12-07 23 views
1

AVCaptureVideoPreviewLayer가있는 UIVIew를 하위 레이어로 회전하고 싶습니다.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

if let videoPreviewLayerConnection = videoPreviewLayer.connection { 
    videoPreviewLayerConnection.videoOrientation = desiredOrientation 
} 
를 시도