Search code examples
iosobjective-cavcapturesessionavcapturedeviceavcapture

How to check the auto focus is still going on in IOS?


I created camera app now what i want to do is need to check is the focus going on continuously or stopped.

This is what i tried :

 NSError *error=nil;
    //Capture Session
    AVCaptureSession *session = [[AVCaptureSession alloc]init];
    session.sessionPreset = AVCaptureSessionPresetPhoto;

    //Add device
    AVCaptureDevice *device =
    [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    // SETUP FOCUS MODE
    if ([device lockForConfiguration:nil]) {

        [device setFocusMode:AVCaptureFocusModeAutoFocus];

        [device unlockForConfiguration];
    }
    else{
        NSLog(@"error while configuring focusMode");
    }
    NSLog(@"%@" ,NSStringFromCGPoint(device.focusPointOfInterest));
    if ([device isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus] && [device lockForConfiguration:&error]){
        [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
        if ([device isFocusPointOfInterestSupported])
            [device setFocusPointOfInterest:CGPointMake(0.5f,0.5f)];
        [device unlockForConfiguration];
    }
    else{
        NSLog(@"problem ");
    }

    //Input
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];

    if (!input)
    {
        NSLog(@"No Input");
    }

    [session addInput:input];

    //Preview Layer
    AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
    UIView *myView = viewForCamera;
    previewLayer.frame = myView.bounds;
    previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    [viewForCamera.layer addSublayer:previewLayer];

    //Start capture session
    [session startRunning];

I searched is there any delegate method but i didn't get ,Please help me to solve this problem .


Solution

  • I got the solution.I tried with sample.I googled and went through lot of answers.

    We must set the point of interest before calling setFocusMode

    In your coding you have not called the point of interest

    if ([device isFocusPointOfInterestSupported])
    {
       [device setFocusPointOfInterest:CGPointMake(0.5f,0.5f)];
       [device setFocusMode:AVCaptureFocusModeAutoFocus];
                 //OR
       [device setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
       [device unlockForConfiguration];
    }
    

    AutoFocus is not working

    Then finding whether the Auto focus is going or not

    Auto Focus Observer