需要解释旋转AV摄像机的步骤

时间:2015-04-30 22:03:59

标签: swift ios8 avfoundation

所以我遇到旋转问题。我有一台AV摄像机设置为在视图控制器中显示。现在我知道有很多组件进入了这个,这就是为什么我只是简单地说AV Camera。我的问题是,当我旋转设备时,预览图层也不会随设备一起旋转。除了任何类型的按钮或录制动作之外,这只是预览要捕获的内容。

我已经看过这里和互联网,但我找不到任何可以简单地向我解释旋转时使用设备正确旋转此预览所涉及的步骤。我发现的所有信息都是点点滴滴,而且很难尝试理解这些代码片段如何被修改为我的逻辑。

如果能够通过我的设备正确旋转此预览图,我可以逐步获得逻辑基础,这将是非常好的。非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:2)

我已经弄明白了!我只需要应用我们在线性代数中学到的标准变换旋转矩阵!在videoPreviewLayer中注册设备旋转通知后,此功能会在设备旋转时旋转viewWillAppear

虽然旋转不顺畅。您可以在设备旋转时看到图层的边缘。我已经尝试将它分派到自己的队列中,但它并没有太大的区别。

我的解决方案是将预览图层锁定到一个方向UIDeviceOrientation.Portrait,然后将deviceOrientationDidChange()应用于我在视图中的按钮。我怀疑当我按下捕获时我将不得不将其应用于视频/图片数据,以便用户能够以他们拍摄视频/图片的方式最终查看它。 (如果我错了,请纠正我)

/**************************************************************************
    DEVICE ORIENTATION DID CHANGE
    **************************************************************************/
    func deviceOrientationDidChange() {

        println("DEVICE ORIENTATION DID CHANGE CALLED")
        let orientation: UIDeviceOrientation = UIDevice.currentDevice().orientation

        //------ IGNORE THESE ORIENTATIONS ------
        if orientation == UIDeviceOrientation.FaceUp || orientation == UIDeviceOrientation.FaceDown || orientation == UIDeviceOrientation.Unknown || orientation == UIDeviceOrientation.PortraitUpsideDown || self.currentOrientation == orientation {
            println("device orientation is \(orientation) --- returning...")
            return
        }


        self.currentOrientation = orientation


        //------ APPLY A ROTATION USING THE STANDARD ROTATION TRANSFORMATION MATRIX in R3 ------
        /*

             x     y      z
           ---           ---
        x | cosø  -sinø   0 |
        y | sinø  cosø    0 |
        z | 0     0       1 |
           ---           ---

        BUT IMPLEMENTED BY APPLE AS

            x       y       z
           ---            ---
        x | cosø    sinø    0 |
        y | -sinø   consø   0 |
        z | 0       0       1 |
            ---            ---
        */

        //----- PERFORM VIDEO PREVIEW LAYER ROTATION BEFORE CAMERA CONTROLLER ROTATION ------

        switch orientation {

        case UIDeviceOrientation.Portrait:
            println("Device Orientation Portrait")
            if self.usingFrontCamera == true {
            }
            else {
                self.playBackTransformation = CGAffineTransformMakeRotation(self.degrees0)
                self.videoPreviewLayer?.setAffineTransform(self.playBackTransformation!)
                self.videoPreviewLayer!.frame = self.view.bounds
            }
            break
        case UIDeviceOrientation.LandscapeLeft:
            println("Device Orientation LandScapeLeft")
            if self.usingFrontCamera == true {
            }
            else {
                self.playBackTransformation = CGAffineTransformMakeRotation(CGFloat(-self.degrees90))
                self.videoPreviewLayer?.setAffineTransform(self.playBackTransformation!)
                self.videoPreviewLayer!.frame = self.view.bounds
            }
            break
        case UIDeviceOrientation.LandscapeRight:
            println("Device Orientation LandscapeRight")
            if self.usingFrontCamera == true {
            }
            else {
                self.playBackTransformation = CGAffineTransformMakeRotation(self.degrees90)
                self.videoPreviewLayer?.setAffineTransform(self.playBackTransformation!)
                self.videoPreviewLayer!.frame = self.view.bounds
            }
            break
        default:
            break
        }
    }