IOS 监听相机对焦事件

 

来自官方文档:

You can use the adjustingFocus property to determine whether a device is currently focusing. You can observe the property using key-value observing to be notified when a device starts and stops focusing.

 

来自StackoverFlow:

// 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"];

   
(...)
}
原文地址:https://www.cnblogs.com/cokecoffe/p/2598955.html