How to turn flashlight ON and OFF in swift?

前端 未结 11 1490
青春惊慌失措
青春惊慌失措 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 15:56

    Swift 4.2

    if let device = AVCaptureDevice.default(for: AVMediaType.video) {
    
        if (device.hasTorch) {
            do {
                try device.lockForConfiguration()
                let torchOn = !device.isTorchActive
                try device.setTorchModeOn(level: 1.0)
                device.torchMode = torchOn ? AVCaptureDevice.TorchMode.on : AVCaptureDevice.TorchMode.off
                device.unlockForConfiguration()
            } catch {
                print(error.localizedDescription)
            }
        }
    }
    

提交回复
热议问题