gpt4 book ai didi

swift - 正在刷新 AVCaptureSession...?

转载 作者:行者123 更新时间:2023-11-30 12:01:18 25 4
gpt4 key购买 nike

弹出 View Controller 时,我在使用 AVCaptureSession 时遇到一些困难。我在导航 Controller 中有一个 View Controller ,用户可以在其中拍照。捕获照片后,我转到“预览照片” View Controller 。如果用户不喜欢这张照片,他们可以返回重新拍摄。当我弹出预览照片 View Controller 时,应用程序崩溃并显示错误“当前不支持多个音频/视频 AVCaptureInputs”

我想也许我可以删除/刷新输入 session ,但它仍然崩溃。

非常感谢任何支持/建议!

继续:

 @IBAction func cancelPressed(_ sender: UIButton) {
_ = self.navigationController?.popViewController(animated: true)
}

相机配置(工作正常):

func setupCaptureSessionCamera() {
//this makes sure to get full res of camera
captureSession.sessionPreset = AVCaptureSession.Preset.photo

var devices = AVCaptureDevice.devices(for: .video)

//query available devices
for device in devices {

if device.position == .front {
frontFacingCamera = device
} else if device.position == .back {
backFacingCamera = device
}
}//end iteration

//set a default device
currentDevice = backFacingCamera

//configure session w output for capturing still img
stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput?.outputSettings = [AVVideoCodecKey : AVVideoCodecType.jpeg]


do {

let captureDeviceInput = try AVCaptureDeviceInput(device: currentDevice!)

captureSession.addInput(captureDeviceInput)
captureSession.addOutput(stillImageOutput!)

//setup camera preview layer
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

//add the preview to our specified view in the UI
view.layer.addSublayer(cameraPreviewLayer!)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.frame = cameraView.frame

captureSession.startRunning()

} catch let error {

print(error)

}//end do
}

我尝试过的(如果发送者是预览照片 Controller ,则会出现删除 View 中的输入):

func refreshCamera() {

captureSession.beginConfiguration()

for input in captureSession.inputs {

captureSession.removeInput(input as! AVCaptureDeviceInput)

}
captureSession.commitConfiguration()

}

最佳答案

这比我想象的要简单得多。只需要在调用 setupCameraSession 方法之前先检查是否已经有输入:

if captureSession.inputs.isEmpty {

setupCaptureSessionCamera()

}

关于swift - 正在刷新 AVCaptureSession...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47190088/

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