当前位置 : 主页 > 手机开发 > ios >

ios – 尝试拍照时传递的无效/无效连接

来源:互联网 收集:自由互联 发布时间:2021-06-11
我试图使用自定义视图拍摄照片(不使用 UIImagePickerController ),但每当我尝试拍照时,应用程序崩溃,并抛出此错误: Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[AVC
我试图使用自定义视图拍摄照片(不使用 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()函数中的连接变量设置为:

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

设置摄像头时,请在AVCaptureSession中添加一个stillImageOutput.

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
        })
    }
}
网友评论