iphone camera notifications observer-pattern autofocus

iPhone: cámara de observación de enfoque automático?



camera notifications (3)

Me gustaría saber si es posible recibir una notificación sobre el enfoque automático dentro de una aplicación de iPhone.

IE, ¿existe una forma de ser notificado cuando se inicia, finaliza el enfoque automático, si ha tenido éxito o ha fallado ...?

Si es así, ¿cuál es el nombre de esta notificación?


Encuentro la solución para mi caso para encontrar cuando el enfoque automático comienza / termina. Es simplemente tratar con KVO (Key-Value Observing).

En mi 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"]; (...) }

Documentación:


Puede usar el valor de la clave Swift moderna observando la api para obtener devoluciones de llamada cuando el enfoque comienza y termina observando la propiedad AVCaptureDeviceInput.device.isAdjustingFocus . En el siguiente ejemplo, la instancia de AVCaptureDeviceInput se llama captureDeviceInput .

Ejemplo:

self.focusObservation = observe(/.captureDeviceInput.device.isAdjustingFocus, options: .new) { _, change in guard let isAdjustingFocus = change.newValue else { return } print("isAdjustingFocus = /(isAdjustingFocus)") }


Swift 3

Establezca el modo de enfoque en su instancia de AVCaptureDevice :

do { try videoCaptureDevice.lockForConfiguration() videoCaptureDevice.focusMode = .continuousAutoFocus videoCaptureDevice.unlockForConfiguration() } catch {}

Añadir observador:

videoCaptureDevice.addObserver(self, forKeyPath: "adjustingFocus", options: [.new], context: nil)

Anular el valor de observeValue :

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { guard let key = keyPath, let changes = change else { return } if key == "adjustingFocus" { let newValue = changes[.newKey] print("adjustingFocus /(newValue)") } }