我们的iOS应用程序具有条形码扫描功能,我们使客户能够根据需要打开和关闭割炬。在AvCaptureSession运行且启用了割炬的情况下,在iPhone X上(仅在iPhone X上),屏幕上的视频捕获会冻结。一旦割炬再次关闭,视频捕获就会再次开始。有人碰到这个吗?我似乎找不到任何可以解决问题的方法。想知道这是否是iPhone X的错误吗?
答案 0 :(得分:2)
我遇到了这个问题。经过一些试验,结果表明,获取设备以配置割炬的操作必须与配置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函数中某些代码可能是多余的,但我将其保留。希望这会有所帮助。