2012-02-01 22 views
16

Bir iPhone uygulamasında otomatik netleme hakkında bildirim almanın mümkün olup olmadığını bilmek ister misiniz?iPhone: kamera otofokus gözlemcisi?

Otomatik odaklama başladığında, bittiğinde, başarılı olursa veya başarısız olduğunda IEC, bildirilmesinin bir yolu var mı?

Öyleyse, bu bildirim adı nedir?

cevap

42

Otofokusun ne zaman başladığını/sona erdiğini bulmak için durumumun çözümünü buluyorum. Sadece KVO (Key-Value Observing) ile uğraşıyor.

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

    (...) 
} 

Belgeleme:

+0

Bu da, otomatik odaklama başarısız olduğunu söylemez. AdjustFocus ayarı yanlış olsa bile, kameranın odakta olduğu anlamına gelmez. –

+1

Bu yöntem, iPhone 6/6Plus/6S/6S Plus sınıfı aygıtlarda da başarısız oluyor, çünkü ayarlamaFocus'un doğru olmadığı farklı bir otomatik netleme modu var. –

+0

ISO anahtarının değeri nedir? – Nil

1

Swift 3

Benim UIViewController içinde

senin AVCaptureDevice örneğinde

Seti odak modu:

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

gözlemci ekleyin:

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

geçersiz kılma 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)") 
    } 
} 
İlgili konular