AVCaptureVideoPreviewLayer从视频切换并在静止图像模式下拍摄图像时的延迟

时间:2018-11-22 10:46:18

标签: ios swift

我正在使用一个库来使用相机和视频https://github.com/yonat/CameraBackground在我的swift应用上创建自定义相机

但是我有点延迟,并且当闪光灯设置关闭时,相机会使用闪光灯拍摄图像 在将摄像机的模式从视频切换为静止图像时,我试图在更改输出后拍摄图像

不幸的是,如果我不设置延迟,它将返回深色图像。

代码:

@IBAction func startCapture(_ sender: Any) {
    DispatchQueue.background(delay: 0.1, background: {
        // do something in background
        let currentSession =  self.previewLiveCamera.cameraLayer?.session
        currentSession!.sessionPreset = AVCaptureSession.Preset.photo
        currentSession!.addOutput(AVCaptureStillImageOutput())


    }, completion: {
        // when background job finishes, wait 3 seconds and do something in main thread

        self.previewLiveCamera.takeCameraSnapshot( {
            // animate snapshot capture
            print("Starting...")
        },
          completion: { (capturedImage, error) -> () in
            if capturedImage != nil {
                print("compleated with image")
                RZTransitionsManager.shared().defaultPresentDismissAnimationController = RZZoomAlphaAnimationController()
                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let nextViewController = storyboard.instantiateViewController(withIdentifier: "post") as! PostViewController
                nextViewController.image = capturedImage
                nextViewController.transitioningDelegate = RZTransitionsManager.shared()
                self.present(nextViewController, animated: true, completion: {
                    self.previewLiveCamera.removeCameraBackground()
                    self.addvideoCamBackground()
                    self.hideCaptureBtns()
                    self.progressView.popIn()
                    self.progressView.transform = CGAffineTransform(rotationAngle: .pi)
                })

            }

        })
    })

}

1 个答案:

答案 0 :(得分:0)

CameraBackground自述文件的用法:

view.addCameraBackground()
// ...
view.takeCameraSnapshot( {
    // animate snapshot capture
    self.view.alpha = 0
    UIView.animate(withDuration: 1) { self.view.alpha = 1 }
}, completion: { (capturedImage, error) -> () in
    self.view.freeCameraSnapshot() // unfreeze image
    // ... handle capturedImage and error
})
// ...
view.removeCameraBackground()

您可以看到,在此用法中,此间隔用于设置可见性,因此,如果您从代码中删除延迟,则alpha保持为0。因此,请使用它在GitHub中的代码描述方式:

view.addCameraBackground()

view.takeCameraSnapshot( {
    print("Starting...")
    self.view.alpha = 0
    UIView.animate(withDuration: 1) { self.view.alpha = 1 }
}, completion: { (capturedImage, error) -> () in
    self.view.freeCameraSnapshot()
    if capturedImage != nil {
        print("compleated with image")
        RZTransitionsManager.shared().defaultPresentDismissAnimationController = RZZoomAlphaAnimationController()
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyboard.instantiateViewController(withIdentifier: "post") as! PostViewController
        nextViewController.image = capturedImage
        nextViewController.transitioningDelegate = RZTransitionsManager.shared()
        self.present(nextViewController, animated: true, completion: {
            self.previewLiveCamera.removeCameraBackground()
            self.addvideoCamBackground()
            self.hideCaptureBtns()
            self.progressView.popIn()
            self.progressView.transform = CGAffineTransform(rotationAngle: .pi)
        })

    }
})

view.removeCameraBackground()