如何在自动闪光模式下由于光线不足而在闪光灯开启或关闭时收到通知

时间:2016-07-27 06:54:03

标签: ios swift flash camera uiimagepickercontroller

使用UIImagePickerController呈现相机并最初将闪光模式设为自动。

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

i see a yellow flash icon appear and disappear according to the lighting in the bottom upper to the capture button.

我想根据灯光将闪光灯设置为ON或关闭时收到通知。

1 个答案:

答案 0 :(得分:1)

只需使用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.