2012-02-01 31 views
16

Tôi muốn biết liệu có thể nhận thông báo về tự động lấy nét bên trong ứng dụng iPhone không?iPhone: máy ảnh tự động lấy nét quan sát?

I.E, nó có tồn tại một cách để được thông báo khi tự động lấy nét bắt đầu, kết thúc, nếu nó thành công hay thất bại ...?

Nếu có, tên thông báo này là gì?

Trả lời

42

Tôi tìm giải pháp cho trường hợp của mình để tìm khi tự động lấy nét bắt đầu/kết thúc. Nó chỉ đơn giản là giao dịch với KVO (Key-Value Observing).

Trong UIViewController tôi:

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

    (...) 
} 

Tài liệu:

+0

này không cho bạn biết liệu tự động lấy nét thất bại, mặc dù. Ngay cả khi AdjustFocus trở thành sai, nó không nhất thiết có nghĩa là máy ảnh đang được lấy nét. –

+1

Phương pháp này cũng không thành công trên các thiết bị lớp 6/6Plus/6S/6S Plus của iPhone, vì có chế độ lấy nét tự động khác nhau khi AdjustFocus không chính xác. –

+0

Giá trị của khóa ISO là gì? – Nil

1

Swift 3

Set chế độ tập trung vào AVCaptureDevice dụ của bạn:

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

Thêm quan sát viên:

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

Override 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)") 
    } 
} 
Các vấn đề liên quan