2012-02-01 14 views
16

Chciałbym wiedzieć, czy możliwe jest otrzymywanie powiadomień o autofokusie w aplikacji na telefon iPhone?iPhone: obserwator autofokusa?

I.E, czy istnieje sposób powiadamiania, gdy autofokus zaczyna się, kończy, czy zakończył się sukcesem, czy nie ...?

Jeśli tak, co to jest nazwa powiadomienia?

Odpowiedz

42

Znajduję rozwiązanie dla mojej sprawy, aby znaleźć, kiedy autofokus zaczyna/kończy się. Po prostu zajmuje się KVO (Key-Value Observing).

W moim 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"]; 

    (...) 
} 

Dokumentacja:

+0

nie powiedzieć, czy autofocus nie powiodła się, choć. Nawet jeśli regulacja ostrości staje się fałszywa, nie musi to oznaczać, że kamera jest ostra. –

+1

Ta metoda nie działa również na urządzeniach iPhone 6/6 Plus/6S/6S Plus, ponieważ istnieje inny tryb autofokusa, w którym regulacja ostrości nie jest dokładna. –

+0

Jaka jest wartość klucza ISO? – Nil

1

Swift 3

Ustaw tryb ustawiania ostrości na przykład AVCaptureDevice:

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

Dodaj obserwatora:

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

Zastąp 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)") 
    } 
} 
Powiązane problemy