본문 바로가기

프로그래밍/iOS

[ios] AVCapturesession auto focus 감지하기

 

1. 등록하기

- (void)viewWillAppear:(BOOL)animated

{

    [super viewWillAppear:animated];

    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    int flags = NSKeyValueObservingOptionNew;

    

    [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];

}


2. 해제하기

- (void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

    [camDevice removeObserver:self forKeyPath:@"adjustingFocus"];

} 


3. 사용하기

#pragma mark - focus observer

- (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] ];

        if (adjustingFocus)

        {

//           포커스 맞추는 중

        }

        else

        {

//            포커스를 맞추고 있지 않는상태 (포커스가 맞춰진 상태)

        }

    }

}