【问题标题】:Crash in AVCaptureDevice temperatureAndTintValuesAVCaptureDevice temperatureAndTintValues 崩溃
【发布时间】:2019-10-28 18:21:31
【问题描述】:

我有以下代码通过 Swift 中的 KVO 观察白平衡变化。

   self.addObserver(self, forKeyPath: "videoInput.device.deviceWhiteBalanceGains", options: [.new, .old], context: &whitebalanceGainsObserverContext)

然后在 observeValue(...) 中,我这样做:

   if context == &whitebalanceGainsObserverContext {
        if let newNSValue = change?[.newKey] as? NSValue {
            var gains = AVCaptureDevice.WhiteBalanceGains()
            newNSValue.getValue(&gains)

            /* Crashes here on some devices in AppStore, throws an exception */
            let newTemperatureAndTint = self.videoInput?.device.temperatureAndTintValues(for: gains)

        }
   }

我永远无法重现崩溃,所以我想知道如何避免崩溃。我要进行哪些检查以避免引发异常?

编辑:我还尝试使用新的观察 API,如下所示:

  deviceWBGainsObservation = observe(\.videoInput?.device.deviceWhiteBalanceGains, options: [.old, .new]) { (obj, change) in

        if let newNSValue = change.newValue {

        }

 }

甚至这个,

  deviceWBGainsObservation = videoDevice?.observe(\.deviceWhiteBalanceGains, options: [.old, .new]) {[unowned self] (object, change) in

               if let newNSValue = change.newValue {

               }


               }

还有这个:

  private var videoDevice:AVCaptureDevice? {

     didSet {
        deviceWBGainsObservation = videoDevice?.observe(\.deviceWhiteBalanceGains, options: [.old, .new]) {[unowned self] (object, change) in

               if let newNSValue = change.newValue {

               }
     }

  }

问题是在这种情况下更改值始终为零。为什么会这样?

【问题讨论】:

  • 如何从 XCode 获取完整的崩溃日志?我只看到回调。
  • 我添加了回溯。
  • 在与 Swift 和 AVFoundation 一起使用之前,我在 observe(_:options:changeHandler:) 中发现了很多错误。这就是我仍然使用observeValue的原因。
  • @matt 你能举例说明我如何将此调用转换为在 Swift 中正常工作的 observable(_:options:changeHandler:) 吗?
  • 啊,因为设备可以在生命周期中随时更改。

标签: ios avfoundation avcapturesession avcapturedevice


【解决方案1】:

这是documentation

如果任何 whiteBalanceGains 结构字段设置为不受支持的值,此方法将引发 invalidArgumentException 异常。

所以看起来您可能会遇到文档明确告诉您可能会遇到的异常。

同一部分告诉您如何避免出现该异常:

对于 whiteBalanceGains 结构中的每个通道,仅支持介于 1.0 和 maxWhiteBalanceGain 之间的值。

您可能需要为此添加检查。

【讨论】:

  • 我知道,但如何从 KVO 越界获得收益是个谜。
  • 嗯。如果您不查看更改字典,而是转而使用原始设备并询问它现在的白平衡增益是多少,这有什么不同吗?
  • 哦,再想一想,小心线程问题。总是担心。
  • 好吧,线程安全是个问题,问题可能是 videoDevice 在其他地方发生了变化,但 KVO 仍然适用于旧的 videoDevice。但是我怎么知道 KVO 是针对哪个对象,旧的还是新的?
  • 我什至不知道这意味着什么。您会收到通知,因为属性已更改。
猜你喜欢
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 2021-05-14
  • 1970-01-01
  • 2016-11-09
  • 2018-12-11
  • 2013-05-30
相关资源
最近更新 更多