AVCaptureVideoPreviewLayer旋转不起作用

时间:2016-07-06 14:53:19

标签: ios swift avcapture

当我将设备从横向左侧旋转到横向右侧或反之亦然时,AVCaptureVideoPreviewLayer会颠倒过来。当我将设备从纵向旋转到横向模式时,它工作得很好。这是我的代码

{    override func viewWillLayoutSubviews() {
        let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
        if(buttonsview.isDescendantOfView(self.view)){
            buttonsview.removeFromSuperview()
        }else{

        }
        switch (orientation)
        {
        case .Portrait:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            buttonsview.layoutSubviews()
            view.addSubview(buttonsview)
             self.activityView.center = self.view.center
            break
        case .LandscapeRight:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            view.addSubview(buttonsview)
             self.activityView.center = self.view.center
            break
        case .LandscapeLeft:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            view.addSubview(buttonsview)
             self.activityView.center = self.view.center
            break

        case .PortraitUpsideDown:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.PortraitUpsideDown
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            buttonsview.layoutSubviews()
            view.addSubview(buttonsview)
            self.activityView.center = self.view.center
            break



        case .Unknown:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeLeft
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            buttonsview.layoutSubviews()
            view.addSubview(buttonsview)
            self.activityView.center = self.view.center
            break

        default:
            previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.Portrait
            previewLayer.frame = self.view.bounds
            var frame = buttonsview.frame
            frame.size.width = 320
            frame.size.height = 50
            let totalWidth : CGFloat = self.view.bounds.size.width
            var x:CGFloat = 0.0;
            if( totalWidth>320)
            {
                x = (totalWidth-320.0)/2.0
            }
            frame.origin.x = x
            frame.origin.y = self.view.bounds.size.height-50
            buttonsview.frame = frame
            buttonsview.layoutSubviews()
            view.addSubview(buttonsview)
             self.activityView.center = self.view.center
            break
        }
    }
}

1 个答案:

答案 0 :(得分:0)

终于做到了。

jData = jData.replace(/updateFrame.+?id/, updateFrame+" "+radios+ id);