17

iPhone アプリケーション内でオートフォーカスに関する通知を受け取ることができるかどうかを知りたいですか?

IE、オートフォーカスの開始時、終了時、成功または失敗したときに通知を受ける方法はありますか...?

もしそうなら、この通知名は何ですか?

4

3 に答える 3

44

私の場合、オートフォーカスの開始/終了のタイミングを見つけるための解決策を見つけました。単純に KVO (Key-Value Observing) を扱っているだけです。

私の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"];

    (...)
}

ドキュメンテーション:

于 2012-02-02T09:39:58.617 に答える
6

スイフト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)")
    }
}
于 2016-11-28T16:18:15.773 に答える