斯威夫特拍照崩溃

时间:2017-10-08 09:01:01

标签: ios swift

我们有一个应用程序,作为更新,我们希望在应用程序中有一个Photobooth。一切正常,我得到相机的预览等,但拍照不起作用..我已经尝试了我在这里找到的所有解决方案,但没有任何作用。

头:

var captureSession: AVCaptureSession?
var videoPreviewLayer: AVCaptureVideoPreviewLayer?

拉​​伸:

do {

        let input = try AVCaptureDeviceInput(device: getDevice(position: .front))

        captureSession = AVCaptureSession()
        captureSession?.addInput(input)

        videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        videoPreviewLayer?.frame = cameraView.bounds
        cameraView.layer.addSublayer(videoPreviewLayer!)

        captureSession?.startRunning()
...

TakePhoto():

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

    let videoConnection = stillImageOutput.connection(withMediaType:AVMediaTypeVideo)

    if (videoConnection?.isEnabled)! {
        stillImageOutput.captureStillImageAsynchronously(from:videoConnection, completionHandler: {
            (sampleBuffer, error) in
...

它在最后一行崩溃了。这是错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] Inconsistent state'

信不信由你,但我们需要在3天内从Apple更新我们的应用程序,否则它将从Appstore中删除(我们需要额外的功能..)所以,如果你能帮助我们,我们将非常感激!

1 个答案:

答案 0 :(得分:0)

我最好的猜测(早上5:30)是您没有要求使用相机作为您的应用。完成此操作后,您需要确保变量stillImageOutput在尝试捕获照片之前响应方法captureStillImageAsynchronouslyFromConnection。如果它没有响应该方法,您的用户将被要求更改他的"设置"允许你使用相机。