Key-Value Observing adjustingFocus 无通知

Key-Value Observing adjustingFocus no notification

我不熟悉 iOS 但我正在尝试查找默认的内置相机应用程序何时对焦。为此,我创建了自己的单独 Objective-C 应用程序并在此处遵循此答案 [iPhone : camera autofocus observer? 但我没有从 NSLog 中的 observeValueForKeyPath 获得任何信息。

#import "ViewController.h"
#import "AVFoundation/AVCaptureDevice.h"
#import "AVFoundation/AVMediaFormat.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"viewDidLoad");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

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

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

@end

非常感谢任何帮助。

对于任何访问此问题的人来说,答案就是 Bluewings 在评论中写的内容。我试图使用 KVO 从一个应用程序观察另一个应用程序,这是不可能的,因为一次只能锁定一个捕获设备。