How to turn flashlight ON and OFF in swift?

前端 未结 11 1491
青春惊慌失措
青春惊慌失措 2020-12-02 15:42

I\'d like to add flashlight functionality to my app in Swift. How can I go about doing that?

11条回答
  •  长情又很酷
    2020-12-02 16:08

    For swift 3

    func toggleFlash() {
        if let device = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo), device.hasTorch {
            do {
                try device.lockForConfiguration()
                let torchOn = !device.isTorchActive
                try device.setTorchModeOnWithLevel(1.0)
                device.torchMode = torchOn ? .on : .off
                device.unlockForConfiguration()
            } catch {
                print("error")
            }
        }
    }
    

提交回复
热议问题