我正在构建一个相机应用程序,并且试图向用户公开当前的曝光时间。由于此值在手动设置之前一直在变化,因此我需要使用kvo将这些值流式传输给用户。我已经使用ISO成功完成此操作,并且可以观察到exposureDuration的更改,但是无法将新值强制转换为CMTime对象(这是ExposureDuration是什么)。以下是我用来尝试完成此操作的代码:

override init() {
  super.init()

  captureDevice = self.selectCamera()

  captureDevice?.addObserver(self, forKeyPath: "ISO", options: .New, context: &isoContext)
  captureDevice?.addObserver(self, forKeyPath: "exposureDuration", options: .New, context: &shutterContext)
}

deinit {
  captureDevice?.removeObserver(self, forKeyPath: "ISO")
  captureDevice?.removeObserver(self, forKeyPath: "exposureDuration")
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
  let newValue = change?[NSKeyValueChangeNewKey]

  if context == &isoContext {
    store.iso.value = newValue as! Float
  } else if context == &shutterContext {
    // The app crashes at this line.
    // Thread 1: EXC_BREAKPOINT (code=1, subcode=0x100091670)
    // newValue is "AnyObject" in the debug area
    store.shutterSpeed.value = newValue as! CMTime
  }
}

我做错什么了吗,或者这是我需要向Apple提交的合法错误?

最佳答案

exposureDuration的newValue不是CMTime,而是NSValue。
这是固定代码(swift3)。

store.shutterSpeed.value = (newValue as! NSValue).timeValue

关于ios - 关键值观察ExposureDuration,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37290457/

10-10 23:11