我正致力于视频应用。我想在相机自动对焦时丢弃视频帧。在自动聚焦期间,捕获的图像变得模糊,并且该帧的图像处理变差,但是一旦完成自动聚焦,图像处理就变得极好。有人给我解决方案吗?
答案 0 :(得分:2)
adjustingFocus
财产。
指示设备当前是否正在调整其焦点设置。 (只读)
*注意:您可以使用键值观察来观察对此属性值的更改。
iOS 4.0及更高版本
答案 1 :(得分:1)
Swift 4+上的示例
class ViewController: UIViewController, AVCapturePhotoCaptureDelegate {
//@objc var captureDevice: AVCaptureDevice?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.addObservers()
}
func addObservers() {
self.addObserver(self, forKeyPath: "captureDevice.adjustingFocus", options: .new, context: nil)
}
func removeObservers() {
self.removeObserver(self, forKeyPath: "captureDevice.adjustingFocus")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "captureDevice.adjustingFocus" {
print("============== adjustingFocus: \(self.captureDevice?.lensPosition)")
}
} //End of class
答案 2 :(得分:0)
以下是Swift 3.x中的示例代码。
首先应在摄像机初始化时将观察者添加到选定的捕获设备。
captureDevice.addObserver(self, forKeyPath: "adjustingFocus", options: [.new], context: nil)
然后重写observeValue方法。通过访问方法返回的可选值,可以识别自动聚焦帧。
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
guard let key = keyPath, let changes = change else {
return
}
if key == "adjustingFocus" {
let changedValue = changes[.newKey]
if (changedValue! as! Bool){
// camera is auto-focussing
}else{
// camera is not auto-focussing
}
}
}