iPhone : camera autofocus observer?

爱⌒轻易说出口 提交于 2019-11-28 05:03:21

I find the solution for my case to find when autofocus starts / ends. It's simply dealing with KVO (Key-Value Observing).

In my UIViewController:

// callback
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if( [keyPath isEqualToString:@"adjustingFocus"] ){
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );
        NSLog(@"Change dictionary: %@", change);
    }
}

// register observer
- (void)viewWillAppear:(BOOL)animated{
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    int flags = NSKeyValueObservingOptionNew; 
    [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];

    (...)   
}

// unregister observer
- (void)viewWillDisappear:(BOOL)animated{
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [camDevice removeObserver:self forKeyPath:@"adjustingFocus"];

    (...)
}

Documentation:

Swift 3

Set focus mode on your AVCaptureDevice instance:

do {
     try videoCaptureDevice.lockForConfiguration()
     videoCaptureDevice.focusMode = .continuousAutoFocus
     videoCaptureDevice.unlockForConfiguration()
} catch {}

Add observer:

videoCaptureDevice.addObserver(self, forKeyPath: "adjustingFocus", options: [.new], context: nil)

Override 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 newValue = changes[.newKey]
        print("adjustingFocus \(newValue)")
    }
}

You can use modern Swift key value observing api to get callbacks when focusing starts and ends by observing AVCaptureDeviceInput.device.isAdjustingFocus property. In the example below, instance of AVCaptureDeviceInput is called captureDeviceInput.

Example:

self.focusObservation = observe(\.captureDeviceInput.device.isAdjustingFocus, options: .new) { _, change in
    guard let isAdjustingFocus = change.newValue else { return }

    print("isAdjustingFocus = \(isAdjustingFocus)")

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!