2012-02-01 163 views
16

我想知道是否有可能收到有關iPhone應用程序內自動對焦的通知?iPhone:相機自動對焦觀察者?

I.E,它是否存在自動對焦開始,結束,成功或失敗時通知的方式......?

如果是這樣,通知名稱是什麼?

回答

42

我找到解決方案爲我的情況找到何時開始/結束自動對焦。它只是處理KVO(鍵值觀測)。

在我的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"]; 

    (...) 
} 

文檔:

+0

這不告訴你自動對焦是否失敗,雖然。即使調節焦點變爲錯誤,也不一定意味着相機對焦。 – 2015-08-05 18:56:24

+1

此方法在iPhone 6/6Plus/6S/6S Plus類設備上也失敗了,因爲有一種不同的自動對焦模式,其中adjustFocus不準確。 – 2016-05-02 18:59:09

+0

ISO的關鍵是什麼? – Nil 2017-06-22 15:17:48

1

斯威夫特3

對焦模式設定在你的AVCaptureDevice實例:

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

添加觀察員:

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

覆蓋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)") 
    } 
}