AVCapture?照片?输出为FlashScene键值观察

Giz*_*odo 2 key-value-coding avfoundation ios swift avcapturephotosettings

我正在关注 Apple 的最新示例代码AVCam Swift,该代码已更新为使用AVCapture\xe2\x80\x8bPhoto\xe2\x80\x8bOutput

\n\n
\n

var isFlashScene: Bool { 获取 }

\n\n

一个布尔值,指示相机当前预览的场景是否允许使用闪光灯。此属性\xe2\x80\x99s 值会根据相机当前可见的场景而变化。例如,您可以使用此属性突出显示应用程序 xe2x80x99s 相机 UI 中的闪光灯控制,向用户表明场景足够暗,可能需要启用闪光灯。如果照片捕获输出\xe2\x80\x99ssupportedFlashModes 值关闭,则此属性\xe2\x80\x99s 值始终为 false。该属性支持键值观察。

\n
\n\n

我正在尝试键值观察这一点,以便当自动闪光模式指示这是闪光灯将闪光的场景时(就像库存的 iOS 相机应用程序一样),所以我可以更改 UI,就像文档注释一样。

\n\n

所以我这样设置:

\n\n
private let photoOutput = AVCapturePhotoOutput()\n\nprivate var FlashSceneContext = 0\n\nself.addObserver(self, forKeyPath: "photoOutput.isFlashScene", options: .new, context: &FlashSceneContext)\n\noverride func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {\n  if context == & FlashSceneContext {\n     print ("Flash Scene Changed")\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

上面从未显示出任何变化。即使我登录查看

\n\n
print (self.photoOutput.isFlashScene)\n
Run Code Online (Sandbox Code Playgroud)\n\n

尽管在应用程序中,这始终显示为 False。

\n\n

我也尝试过:

\n\n
self.photoOutput.addObserver(self, forKeyPath: "isFlashScene", options: .new, context: &FlashSceneContext)\n
Run Code Online (Sandbox Code Playgroud)\n\n

.... Flash 场景仍然没有变化,它停留在 False 上。

\n

Giz*_*odo 5

self.photoOutput.addObserver(self, forKeyPath: "isFlashScene", options: .new, context: &FlashSceneContext)
Run Code Online (Sandbox Code Playgroud)

以上是设置 KVO 的正确方法。

必须实现 photoSettingsForSceneMonitoring:

let photoSettings = AVCapturePhotoSettings()
photoSettings.flashMode = .auto
photoSettings.isAutoStillImageStabilizationEnabled = true
self.photoOutput.photoSettingsForSceneMonitoring = photoSettings
Run Code Online (Sandbox Code Playgroud)

作品!