AVCapture​Photo​Output isFlashScene Key-Value-Observing

527 Views Asked by At

I am following Apple's latest sample code AVCam Swift, which is updated to use AVCapture​Photo​Output.

var isFlashScene: Bool { get }

A Boolean value indicating whether the scene currently being previewed by the camera warrants use of the flash. This property’s value changes depending on the scene currently visible to the camera. For example, you might use this property to highlight the flash control in your app’s camera UI, indicating to the user that the scene is dark enough that enabling the flash might be desirable. If the photo capture output’s supportedFlashModes value is off, this property’s value is always false. This property supports Key-value observing.

I am trying to Key-value observe this so when Auto Flash Mode indicates that this is a scene that flash will fire (Just like the stock iOS Camera App), So I can change the UI, just like the documentation notes.

So I set it up like this:

private let photoOutput = AVCapturePhotoOutput()

private var FlashSceneContext = 0

self.addObserver(self, forKeyPath: "photoOutput.isFlashScene", options: .new, context: &FlashSceneContext)

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  if context == & FlashSceneContext {
     print ("Flash Scene Changed")
  }
}

Above never shows a change. Even if I put a log in to check

print (self.photoOutput.isFlashScene)

This comes out as False all the time though out the app.

I also tried :

self.photoOutput.addObserver(self, forKeyPath: "isFlashScene", options: .new, context: &FlashSceneContext)

.... still no change is Flash Scene, it's stuck on False.

1

There are 1 best solutions below

0
On
self.photoOutput.addObserver(self, forKeyPath: "isFlashScene", options: .new, context: &FlashSceneContext)

Above was the proper way to setup the KVO.

photoSettingsForSceneMonitoring has to be implemented:

let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .auto
photoSettings.isAutoStillImageStabilizationEnabled = true
self.photoOutput.photoSettingsForSceneMonitoring = photoSettings

Works!