Inactive/invalid 尝试拍照时连接成功

Inactive/invalid connection passed when attempting to take a photo

我正在尝试使用自定义视图拍照(不使用 UIImagePickerController),但每当我尝试拍照时,应用程序崩溃,并抛出此错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inactive/invalid connection passed.'

这是我的 takePhoto() 函数,它导致错误:

func takePhoto(sender: UIButton!){
    var still: AVCaptureStillImageOutput = AVCaptureStillImageOutput()
    var connection: AVCaptureConnection = AVCaptureConnection(inputPorts: self.input.ports, output: still)

    if(connection.enabled){
        still.captureStillImageAsynchronouslyFromConnection(connection, completionHandler: {(buffer: CMSampleBuffer!, error: NSError!) -> Void in
            println("picture taken")//this never gets executed
        })
    }
}

我还尝试将 takePhoto() 函数中的 connection 变量设置为:

self.output.connections[0] as AVCaptureConnection

以及

(self.session.outputs[0] as AVCaptureOutput).connections[0] as AVCaptureConnection

我得到了相同的结果。

在与拍照按钮相同的视图中,还有相机的实时预览(有效):

func setupCamera(){
    self.session = AVCaptureSession()
    self.session.sessionPreset = AVCaptureSessionPreset640x480
    self.session.beginConfiguration()
    self.session.commitConfiguration()

    var error: NSError?

    var devices: [AVCaptureDevice] = AVCaptureDevice.devices() as [AVCaptureDevice]
    for device in devices{
        if(device.hasMediaType(AVMediaTypeVideo) && device.supportsAVCaptureSessionPreset(AVCaptureSessionPreset640x480)){

            //the input variable is initialized here

            self.input = AVCaptureDeviceInput.deviceInputWithDevice(device as AVCaptureDevice, error: &error) as AVCaptureDeviceInput

            if(self.session.canAddInput(self.input)){
                self.session.addInput(self.input)
                break
            }
        }
    }

    var settings = [kCVPixelBufferPixelFormatTypeKey:kCVPixelFormatType_32BGRA]

    //the output variable is initialized here

    self.output = AVCaptureVideoDataOutput()
    self.output.videoSettings = settings
    self.output.alwaysDiscardsLateVideoFrames = true

    if(self.session.canAddOutput(self.output)){
        self.session.addOutput(self.output)
    }

    var captureLayer = AVCaptureVideoPreviewLayer(session: self.session)
    captureLayer.frame = CGRectMake(0, 64, self.view.frame.width, (self.view.frame.width * 4) / 3)
    self.view.layer.addSublayer(captureLayer)

    self.session.startRunning()
}

我正在 iPhone 5s 上测试它,一切正常,包括预览,但 takePhoto() 功能除外。

有什么方法可以做到这一点,还是我必须使用 UIImagePickerController

设置相机时,将 stillImageOutput 添加到 AVCaptureSession

self.stillImageOutput = AVCaptureStillImageOutput()
let stillSettings = [AVVideoCodecJPEG:AVVideoCodecKey]
self.stillImageOutput.outputSettings = stillSettings

if(self.session.canAddOutput(self.stillImageOutput)){
    self.session.addOutput(self.stillImageOutput)
}

然后拍照时,从stillImageOutput中获取AVCaptureSession

func takePhoto(sender: UIButton!){
    let connection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo)

    if(connection.enabled){
        self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(connection, completionHandler: {(buffer: CMSampleBuffer!, error: NSError!) -> Void in
            println("picture taken") // Now, this is executed
        })
    }
}

无论谁从 crashlytics 或您无法重现的东西中找到这个问题:

用户拒绝相机访问。检查相机权限是否被授予。我遇到了生产崩溃,原来是这种情况。