7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

カメラのフォーカス状態の取得

Last updated at Posted at 2014-10-31
// カメラデバイスからの入力を作成し、セッションに追加する
NSError *error = nil;
_camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];

if ([_camera lockForConfiguration:&error]) {
    [_camera setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
    _camera.autoFocusRangeRestriction = AVCaptureAutoFocusRangeRestrictionNear;
    if ([_camera isExposureModeSupported:AVCaptureExposureModeAutoExpose]){
        [_camera setExposureMode:AVCaptureExposureModeAutoExpose];
    }
    [_camera unlockForConfiguration];
}

[_camera addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];

// 画面遷移時など不要になったらremoveしないと再度addしたときに落ちる
// [_camera removeObserver:self forKeyPath:@"adjustingFocus"];

/**

  • 状態が変化したときに呼ばれるのはこちら
    */
  • (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) {
          _focusing=true;
          _finishedFocus = false;
          NSLog(@"focusing");
      }
      else {
          if (_focusing) {
              _focusing = false;
              _finishedFocus = true;
              NSLog(@"finishFocus");
          }
      }
    

    }
    }

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?