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

标签 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.

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

最佳答案

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

相关文章:

ios - WKImage 到数据总是返回 nil

iphone - UIPageViewController,如何在不打乱数据源指定顺序的情况下正确跳转到特定页面?

ios - 自定义按钮在 Interface Builder 中未正确显示

swift - 设置 Swift 脚本的框架搜索路径

ios - AS3 在按住按钮的同时连续运行代码 - Air For iOS/Android

ios - 自动收缩导航栏标题 iOS 7 或更高版本

iphone - 单击推送通知的操作按钮时,如何转到最新的 DetailView?

ios - 使用 MKGeodesicPolyline Swift 3 计算距离将出现错误

ios - 闪存临 CS6 : Publishing for iOS using AIR - "Could not access digital certificate"

javascript - 有没有办法将文件附加到已经存在的文件列表中? javascript 文件接口(interface)