AVFoundation相机完成对焦时检测

Zac*_*los 10 iphone avfoundation ios

我目前正在使用AVFoundation来运行我的相机,但我希望能够检测到相机何时完成对焦.有没有办法做到这一点?

非常感谢!!

Shu*_*ank 9

根据文件

您可以使用该adjustingFocus属性来确定设备当前是否正在聚焦.您可以观察该属性key-value observing,以便在设备启动和停止聚焦时收到通知.

如果更改焦点模式设置,则可以将其恢复为默认配置,如下所示:


Ale*_*Akl 6

在初始化中:

[[[captureManager videoInput] device] addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];
Run Code Online (Sandbox Code Playgroud)

然后:

bool focusing = false;  
bool finishedFocus = false;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    if( [keyPath isEqualToString:@"adjustingFocus"] ){
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];

        if (adjustingFocus) {
             focusing=true;
        }
        else {
           if (focusing) {
                focusing = false;
                finishedFocus = true;
           }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)