打开手电筒时AVCaptureSession冻结

sully77

我们的iOS应用程序中具有条形码扫描功能,我们为客户提供了根据需要打开和关闭割炬的功能。在AvCaptureSession运行且启用了割炬的情况下,在iPhone X上(仅在iPhone X上),屏幕上的视频捕获会冻结。一旦割炬再次关闭,视频捕获就会再次开始。有人碰到这个吗?我似乎找不到任何可以解决的问题。想知道这是否是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函数中可能是多余的,但我将其保留。希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章