How to observe torchLevel in swift?

2019-06-05 04:40发布

问题:

How do I observe torchLevel? I have done the following from objective c solutions, no success:

private var torchLevel = 0

let device = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    device.addObserver(self, forKeyPath: "torchLevel", options: .New, context: &torchLevel)

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if context == &torchLevel {
        let device = object as! AVCaptureDevice
        print("level \(device.torchLevel)")
    } else {
        print("Nope")
    }
}

my alternate direction would be to turn torch off and set a variable to 0.0 and update that as the flash is manipulated.