gpt4 book ai didi

ios - AVFoundation 切换相机速度慢(连接到样本缓冲区)

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

override func viewDidLoad() {
super.viewDidLoad()
...
setupDevice()
setupInputOutput()
}

查看加载是否启动设置链

 func setupDevice() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
let devices = deviceDiscoverySession.devices

for device in devices {
if device.position == AVCaptureDevice.Position.back {
backCamera = device
setupCorrectFramerate(currentCamera: backCamera!)
}
else if device.position == AVCaptureDevice.Position.front {
frontCamera = device
setupCorrectFramerate(currentCamera: frontCamera!)
}
}
currentCamera = cameraModeIsBack ? backCamera : frontCamera
}

var deviceInput: AVCaptureDeviceInput?
let videoOutput = AVCaptureVideoDataOutput()

func setupInputOutput() {
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
deviceInput = captureDeviceInput
captureSession.sessionPreset = AVCaptureSession.Preset.hd1280x720

if captureSession.canAddInput(deviceInput!) {
captureSession.addInput(deviceInput!)
}
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "sample buffer delegate", attributes: []))
if captureSession.canAddOutput(videoOutput) {
captureSession.addOutput(videoOutput)
}
captureSession.startRunning()
if(firstTime){
setupMetal()
}
firstTime = false
toggleCamerButton.isEnabled = true
} catch {
print(error)
}
}

在较高级别上,我找到设备输入,设置其帧速率并设置捕获 session (缓冲区输出被处理到 MTKView)。

问题是当我更换相机时。它要么是快速且不连续,要么是变得非常慢并且在响应之前卡住 4-5 秒。

我用来更改相机的逻辑:

func updateCameras(){
DispatchQueue.global().async {
self.captureSession.stopRunning()
self.captureSession.removeInput(self.deviceInput!)
self.currentCamera = self.cameraModeIsBack ? self.backCamera : self.frontCamera
self.changeCameras()
}
}


func changeCameras(){
do {
let captureDeviceInput = try AVCaptureDeviceInput(device: self.currentCamera!)
self.deviceInput = captureDeviceInput
if self.captureSession.canAddInput(self.deviceInput!) {
self.captureSession.addInput(self.deviceInput!)
}
self.captureSession.startRunning()
DispatchQueue.main.async {
self.toggleCamerButton.isEnabled = true
}
} catch {
print(error)
}

}

我已经尽可能地修改了它。切换相机本质上是

  1. 停止摄像头 session
  2. 删除输入
  3. 添加输入
  4. 开始 session

我不知道为什么它的变化如此之大(大部分时间在我的 iPhone X 上运行良好,而在我的 iPhone 6 上却总是很慢)。

最佳答案

我怀疑操作cameraSession的调用应该在主线程上完成。尝试更改 updateCameras() 以使用 DispatchQueue.main.async() 而不是 DispatchQueue.global().async

像这样:

func updateCameras(){
DispatchQueue.main.async {
self.captureSession.stopRunning()
self.captureSession.removeInput(self.deviceInput!)
self.currentCamera = self.cameraModeIsBack ? self.backCamera : self.frontCamera
self.changeCameras()
}
}

完成此更改后,将不再需要 changeCameras() 函数中对 DispatchQueue.main.async() 的调用

关于ios - AVFoundation 切换相机速度慢(连接到样本缓冲区),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52396611/

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