ios - 打开手电筒时 AVCaptureSession 冻结
问题描述
我们的 iOS 应用程序具有条码扫描功能,我们让客户能够根据需要打开和关闭手电筒。在 iPhone X 上(并且仅在 iPhone X 上),当 AvCaptureSession 正在运行并启用了手电筒时,屏幕上的视频捕获会冻结。一旦手电筒再次关闭,视频捕捉就会再次开始。有没有人遇到过这个?我似乎找不到任何指向解决方法的东西。想知道这是否是 iPhone X 的错误?
解决方案
我遇到了这个问题。经过一些实验,事实证明,获取设备来配置手电筒的方式必须与配置 AVCaptureSession 时获取设备的方式完全相同。例如:
let captureSession = AVCaptureSession()
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)
guard let captureDevice = deviceDiscoverySession.devices.first else {
print("Couldn't get a camera")
return
}
do {
let input = try AVCaptureDeviceInput(device: captureDevice)
captureSession!.addInput(input)
} catch {
print(error)
return
}
在打开和关闭手电筒(手电筒)时使用该精确方法获取设备。在这种情况下,这些行:
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)
guard let device = deviceDiscoverySession.devices.first
例子:
func toggleTorch() {
let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInDualCamera], mediaType: AVMediaType.video, position: .back)
guard let device = deviceDiscoverySession.devices.first
else {return}
if device.hasTorch {
do {
try device.lockForConfiguration()
let on = device.isTorchActive
if on != true && device.isTorchModeSupported(.on) {
try device.setTorchModeOn(level: 1.0)
} else if device.isTorchModeSupported(.off){
device.torchMode = .off
} else {
print("Torch mode is not supported")
}
device.unlockForConfiguration()
} catch {
print("Torch could not be used")
}
} else {
print("Torch is not available")
}
}
我意识到在 toggleTorch 函数中有些代码可能是多余的,但我要离开它。希望这可以帮助。
推荐阅读
- php - 将 Imagine 与 Xampp PHP 8.0.1 一起使用时出错
- python-3.x - 在递归函数中返回列表列表
- graphql - 如何仅在 1 个请求中减少 3 个 graphQL Apollo 请求
- javascript - 如何在 JavaScript 中使用切换功能?
- javascript - 将按钮粘贴到页脚 flexbox
- oracle - 如果表的列包含特定值,如何返回表名列表?
- python - 寻求有关使用 Plotly 创建地图的帮助
- python-3.x - Django 用户模型自定义字段名称
- javascript - JavaScript 没有在 HTML 中显示我想要的内容
- python - 使用 Python 在 AWS Lambda 中进行部分日志记录