gpt4 book ai didi

ios - 尝试拍照时传递了不活动/无效的连接

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:28 25 4
gpt4 key购买 nike

我正在尝试使用自定义 View 拍照(不使用 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

我得到了相同的结果。

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

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
})
}
}

关于ios - 尝试拍照时传递了不活动/无效的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28336751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com