如何将AVCaptureSession捕获图像的图像数据传递给另一个视图控制器?

时间:2017-01-26 06:37:42

标签: swift3 avfoundation avcapturesession presentviewcontroller

我使用AVFoundation的AVCaptureSession创建了一个相机应用程序,我想将捕获图像的数据传递给另一个视图控制器。

这是处理捕获操作系统的函数:

func capturePhoto() {

    self.counter += 1

    if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo) {
        videoConnection.videoOrientation = .landscapeRight
        sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (buffer, error) in

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
            let data = NSData(data: imageData!)
            // append the data to a global variable
            DispatchQueue.main.async {
                self.imageData?.append(data)
            }
            let image = UIImage(data: imageData!)!
            UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)                
        })

    }

    if self.counter == self.count {
        self.timer?.invalidate()
        self.timer = nil
        self.counter = 0
        self.shutterTimer.isHidden = true
        self.shutterTimer.layer.removeAllAnimations()

        // if the condition is met, then go to another vc with the image data  
        setupFrameAndSave()
    }

}

这是使用图像数据处理segue到另一个视图控制器的函数:

func setupFrameAndSave() {

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let controller = storyboard.instantiateViewController(withIdentifier: "saveVC") as! SaveVC
    controller.imageData = self.imageData

    self.present(controller, animated: true, completion: nil)

}

现在,问题是传递的数据打印为nil并将附加数据放在DispatchQueue.main.async中的数组中没有做任何事情。

0 个答案:

没有答案