我正在尝试使用自定义视图拍摄照片(不使用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
?
答案 0 :(得分:6)
设置相机时,请将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
})
}
}
答案 1 :(得分:1)
无论是谁从崩溃症中接触到这个问题,还是你无法复制的东西:
用户拒绝了相机访问权限。检查是否授予了相机权限。我遇到了生产崩溃,结果就是这种情况。