我不熟悉iOS,但我正在尝试寻找默认的内置相机应用程序何时对焦。为此,我创建了自己的单独的Objective-C应用程序,并遵循此处的回答[iPhone : camera autofocus observer?](https://stackoverflow.com/questions/9100357/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任何帮助都非常感谢。
发布于 2017-07-20 22:07:32
对于任何访问这个问题的人来说,答案是Bluewings作为评论写的东西。我试图使用KVO从一个应用程序观察另一个应用程序,这是不可能的,因为捕获设备上一次只能有一个锁。
https://stackoverflow.com/questions/44967801
复制相似问题