首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Key-Value观察adjustingFocus无通知

Key-Value观察adjustingFocus无通知
EN

Stack Overflow用户
提问于 2017-07-07 17:52:04
回答 1查看 291关注 0票数 1

我不熟悉iOS,但我正在尝试寻找默认的内置相机应用程序何时对焦。为此,我创建了自己的单独的Objective-C应用程序,并遵循此处的回答[iPhone : camera autofocus observer?](https://stackoverflow.com/questions/9100357/iphone-camera-autofocus-observer),但我在NSLog中没有从observeValueForKeyPath获取任何内容。

代码语言:javascript
复制
#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

任何帮助都非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-20 22:07:32

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44967801

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档