在拍摄视频iOS swift时保持手持火炬

时间:2017-03-22 06:28:18

标签: ios swift camera-flash

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

 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)
        }
    }

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

1 个答案:

答案 0 :(得分:4)

调用此方法

在相机内部激活/打开func或当设备相机处于活动状态时 -

func flashActive() {
    if let currentDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), 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")
        }
    }
}