如何检查自动对焦仍在IOS中进行?

时间:2016-08-10 05:49:03

标签: ios objective-c avcapturesession avcapturedevice avcapture

我现在创建了相机应用程序,我想要做的是检查焦点是持续停止还是停止。

这就是我的尝试:

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

我搜索的是有任何委托方法,但我没有得到,请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

我得到了解决方案。我尝试了样本。我用谷歌搜索并经历了很多答案。

  

我们必须在调用setFocusMode

之前设置兴趣点

在您的编码中,您尚未调用兴趣点

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

AutoFocus is not working

然后查找自动对焦是否正常

Auto Focus Observer