我正在尝试使用自定义视图(不使用UIImagePickerController
)拍照,但每当我尝试拍照时,应用程序崩溃,并引发此错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - 已通过非活动/无效连接。”
这是我的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
})
}
}
我还尝试将函数中的connection
变量设置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
?