swift - 需要说明旋转 AV 摄像机的步骤

标签 swift ios8 avfoundation

所以我遇到了旋转问题。我有一个 AV 摄像机设置为在 View Controller 中显示。现在我知道其中涉及很多组件,这就是为什么我只是说 AV 摄像机。我的问题是,当我旋转设备时,预览层也不随设备旋转。除了具有任何按钮或任何类型的记录操作之外,这只是对要捕获的内容的预览。

我到处查看了这里和互联网,但我找不到任何可以简单地向我解释在设备旋转时正确旋转此预览所涉及的步骤的内容。我找到的所有信息都是零碎的,很难理解如何根据我的逻辑修改这些代码片段。

如果我能在我需要用我的设备正确旋转这个预览层的地方逐步获得逻辑基础,那就太好了。非常感谢任何帮助。

谢谢

最佳答案

我想通了!我只需要应用我们在线性代数中学到的标准变换旋转矩阵!此函数在 viewWillAppear 中注册设备旋转通知后,随设备旋转旋转 videoPreviewLayer

虽然旋转并不顺畅。您可以看到层随设备旋转时的边缘。我试过将它分派(dispatch)到自己的队列中,但这并没有太大的区别。

我的解决方案是将预览层锁定到一个方向,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
        }
    }

关于swift - 需要说明旋转 AV 摄像机的步骤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29978627/

相关文章:

ios - 根据 URL 更改 UIImageView

ios - UITextView 行高

ios - 如何检查 "Allow Full Access"自 iOS 8.3 *从容器应用程序*被授予

ios - 终止应用程序后从原来的位置播放视频

ios - 以编程方式使用日期选择器更改按钮标题时,它始终会重置

swift - 我不断收到 "Swift Dynamic Cast Failed"。我如何解决它?

iOS 7 AVCaptureMetadataOutput 委托(delegate)(QRCode 扫描仪)

ios - AvQueuePlayer 不在 iPhone 上播放音频,只能在模拟器上工作

iphone - WatchKit:WatchKit 应用程序中的语音到文本转换

iOS 8 top layout guide自动布局问题