iPhone : camera autofocus observer?

后端 未结 3 496
迷失自我
迷失自我 2020-12-08 05:38

I would like to know if it\'s possible to receive notification about autofocus inside an iPhone application?

I.E, does it exist a way to be notified when autofocus s

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 06:02

    I find the solution for my case to find when autofocus starts / ends. It's simply dealing with KVO (Key-Value Observing).

    In my UIViewController:

    // callback
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if( [keyPath isEqualToString:@"adjustingFocus"] ){
            BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
            NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );
            NSLog(@"Change dictionary: %@", change);
        }
    }
    
    // register observer
    - (void)viewWillAppear:(BOOL)animated{
        AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        int flags = NSKeyValueObservingOptionNew; 
        [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];
    
        (...)   
    }
    
    // unregister observer
    - (void)viewWillDisappear:(BOOL)animated{
        AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
        [camDevice removeObserver:self forKeyPath:@"adjustingFocus"];
    
        (...)
    }
    

    Documentation:

    • Key-Value Observing programming guide
    • NSKeyValueObserving protocol

提交回复
热议问题