我正在使用UIImagePickerController演示相机,并最初将闪光模式设置为“自动”。

videoCapturer.sourceType = UIImagePickerControllerSourceType.Camera
videoCapturer.mediaTypes = [kUTTypeMovie as String]
videoCapturer.cameraFlashMode = UIImagePickerControllerCameraFlashMode.Auto
[self .presentViewController(videoCapturer, animated: true, completion: nil)]

ios - 由于光线不足,如何在自动闪光模式下开启或关闭闪光灯时得到通知-LMLPHP

我想根据灯光将闪光灯设置为打开或关闭时得到通知。

最佳答案

只需使用KVO。

let capture = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
capture.addObserver(self, forKeyPath: "torchActive", options: NSKeyValueObservingOptions.New.union(.Initial), context: nil)

并实现此方法:
public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    if keyPath == "torchActive" {
        // do something when torchActive changed
    } else {
        super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
    }
}

这是Apple对torchActive的描述:
 @property torchActive
 @abstract
     Indicates whether the receiver's torch is currently active.

 @discussion
     The value of this property is a BOOL indicating whether the receiver's torch is
     currently active. If the current torchMode is AVCaptureTorchModeAuto and isTorchActive
     is YES, the torch will illuminate once a recording starts (see AVCaptureOutput.h
     -startRecordingToOutputFileURL:recordingDelegate:). This property is key-value observable.

关于ios - 由于光线不足,如何在自动闪光模式下开启或关闭闪光灯时得到通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38605746/

10-09 13:02