打开手电筒时 AVCaptureSession 冻结
AVCaptureSession freezes when torch is turned on
我们的 iOS 应用程序中有条形码扫描功能,我们让客户能够根据需要打开和关闭手电筒。在 iPhone X 上(并且仅在 iPhone X 上)当 AvCaptureSession 为 运行ning 并且手电筒已启用时,屏幕上的视频捕获会冻结。一旦手电筒再次关闭,视频捕捉就会再次开始。有人 运行 参与其中吗?我似乎找不到任何指向解决方法的东西。想知道这是否是 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 函数中的某些代码可能是多余的,但我将保留它。希望这有帮助。
我们的 iOS 应用程序中有条形码扫描功能,我们让客户能够根据需要打开和关闭手电筒。在 iPhone X 上(并且仅在 iPhone X 上)当 AvCaptureSession 为 运行ning 并且手电筒已启用时,屏幕上的视频捕获会冻结。一旦手电筒再次关闭,视频捕捉就会再次开始。有人 运行 参与其中吗?我似乎找不到任何指向解决方法的东西。想知道这是否是 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 函数中的某些代码可能是多余的,但我将保留它。希望这有帮助。