AVCaptureVideoPreviewLayer旋转不起作用

Beamdev1

当我将设备从左横向旋转到右横向或反之时,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
        }
    }
}
Beamdev1

终于做到了。

override func viewDidLayoutSubviews() {
        var newOrientation: AVCaptureVideoOrientation
        var orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
        switch (orientation) {
        case .Portrait:
            newOrientation = .Portrait;
            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 .PortraitUpsideDown:
            newOrientation = .PortraitUpsideDown;
            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 .LandscapeLeft:
            newOrientation = .LandscapeRight;
            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 .LandscapeRight:
            newOrientation = .LandscapeLeft;

            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;
        default:
            newOrientation = .Portrait;
            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
          }

        let newSize = self.view.bounds.size;
        previewLayer.position = CGPointMake(0.5 * newSize.width, 0.5 * newSize.height);
    }

    override func willAnimateRotationToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
        CATransaction.begin()
        CATransaction.setAnimationDuration(duration)
        CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut));
        updatePreviewLayerForOrientation(toInterfaceOrientation);
        CATransaction.commit();
    }

    func updatePreviewLayerForOrientation(interfaceOrientation: UIInterfaceOrientation)
    {
        // Correct position of previewLayer
        let newSize = view.bounds.size;
        previewLayer.position = CGPointMake(0.5 * newSize.width, 0.5 * newSize.height);
        var angle: Double!
        // Rotate the previewLayer, in order to have camera picture right
        switch interfaceOrientation {
        case .Portrait:
            angle = 0


        case .LandscapeLeft:
            angle = M_PI/2
        case .LandscapeRight:
            angle = -M_PI/2
        default: // .PortraitUpsideDown
            angle = M_PI
        }

        previewLayer.setAffineTransform(CGAffineTransformMakeRotation(CGFloat(angle)))
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章