【问题标题】:Key-Value Observing adjustingFocus no notificationKey-Value Observing 调整焦点无通知
【发布时间】:2017-07-07 09:52:03
【问题描述】:

我不熟悉 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

非常感谢任何帮助。

【问题讨论】:

  • 你能在你的视图控制器上进行实时摄像机预览吗?根据您提供的代码,您似乎只是创建了 AVCaptureDevice 实例而没有做任何事情。
  • @Bluewings 我想在本机 iPhone 相机应用程序使用相机时观察adjustingFocus 键/值,所以我没有在我自己的代码中创建预览。我们可以使用 KVO 来观察其他应用程序的键吗?
  • 不,您无法从其他应用程序中观察到。根据文档,您必须获得使用任何 AVCaptureDevice 的锁定才能使用或操作它。因此,当其他应用程序使用任何捕获设备时,您实际上无法了解它。

标签: objective-c camera ios9 key-value-observing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多