关键值观察exposureDuration

时间:2016-05-18 04:41:45

标签: ios swift key-value-observing avcapturedevice

我正在构建一个相机应用程序,我正在尝试向用户公开当前的曝光持续时间。由于此值经常更改,直到手动设置,我需要使用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
  }
}

我做错了什么,或者这是我需要用苹果提交的合法错误?

1 个答案:

答案 0 :(得分:0)

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

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