AVFoundation图像捕获信号SIGBART错误

时间:2017-01-19 15:51:39

标签: swift swift3 avfoundation

尝试使用AVFoundation捕获图像时出现signal SIGBART错误。它应该自动拍摄图像而不会显示任何视图,这个想法是捕获一个稍微不讨好的图像。

func captureImage() {
    let captureSession = AVCaptureSession()

    let stillImageOutput = AVCaptureStillImageOutput()
    stillImageOutput.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
    if captureSession.canAddOutput(stillImageOutput) {
        captureSession.addOutput(stillImageOutput)
    }

    captureSession.sessionPreset = AVCaptureSessionPresetHigh
    let devices = AVCaptureDevice.devices()

    for device in devices as! [AVCaptureDevice] {
        if device.position == .front {
            let input = try! AVCaptureDeviceInput(device: device)
            if captureSession.canAddInput(input) {
                captureSession.addInput(input)
                self.beginSession(stillImageOutput: stillImageOutput)
            }
        }
    }
}

func beginSession(stillImageOutput: AVCaptureStillImageOutput) {
    if let videoConnection = stillImageOutput.connection(withMediaType: AVMediaTypeVideo){
        stillImageOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: { (sampleBuffer, error) in
            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
            let dataProvider = CGDataProvider(data: imageData as! CFData)
            let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
            self.profileImage.image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.up)


            UIImageWriteToSavedPhotosAlbum(self.profileImage.image!, nil, nil, nil)

        })
    }
}

错误发生在beginSession函数的第一行和第二行。它还说libc++abi.dylib: terminating with uncaught exception of type NSException

我从未做过这样的事情,所以它可能是基本的东西。任何帮助都会很棒。感谢

0 个答案:

没有答案