拍摄视频时保持手电筒开启 iOS swift

Keep torch on while taking video iOS swift

我构建了一个用于自动捕捉的相机应用程序。只要相机开着,我就想保持闪光灯开着。我设置了以下代码:

 cameraDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
    if (cameraDevice.hasTorch) {
        do {
            try cameraDevice.lockForConfiguration()

            if cameraDevice.isTorchActive {
                cameraDevice.torchMode = AVCaptureTorchMode.on

            } else {
                // sets the torch intensity to 100%
               try  cameraDevice.setTorchModeOnWithLevel(0.8)
            }

            cameraDevice.unlockForConfiguration()
        } catch {
            print(error)
        }
    }

但是当我运行这个应用程序时,它只闪烁一次然后就熄灭了。我该如何解决这个问题?

调用这个方法

在您的相机内 active/Open func 或当设备相机处于活动状态时 -

   func flashActive() {
    if let currentDevice = AVCaptureDevice.default(for: AVMediaType.video), currentDevice.hasTorch {
        do {
            try currentDevice.lockForConfiguration()
            let torchOn = !currentDevice.isTorchActive
            try currentDevice.setTorchModeOn(level:1.0)//Or whatever you want
            currentDevice.torchMode = torchOn ? .on : .off
            currentDevice.unlockForConfiguration()
        } catch {
            print("error")
        }
    }
}