3

I used the below code for focusing the iphone camera. But it is not working. I take this code from the AVCam sample code of Apple. Am I doing anything wrong? Is there any method to detect if the iPhone did focussing?

-(void) focusAtPoint:(CGPoint)point   
{
    AVCaptureDevice *device =  [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];;

    if (device != nil) {            
        if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {

            NSError *error;

            if ([device lockForConfiguration:&error]) {             
                [device setFocusPointOfInterest:point];             
                [device setFocusMode:AVCaptureFocusModeAutoFocus];              
                [device unlockForConfiguration];                
            } else {                
                NSLog(@"Error in Focus Mode");
            }        

        }
    }
}
Nate
  • 31,017
  • 13
  • 83
  • 207
isarathg
  • 858
  • 1
  • 19
  • 37
  • possible duplicate of [iOS 5 - AVCaptureDevice setting focus point and focus mode freezes the live camera picture](http://stackoverflow.com/questions/7919485/ios-5-avcapturedevice-setting-focus-point-and-focus-mode-freezes-the-live-came) – tc. May 06 '13 at 12:16

1 Answers1

7

The point for [AVCaptureDevice setFocusPointOfInterest:] is not the same as the touch point of the view, but a point between 0,0 and 1,1 (see docs). So you have to do:

CGPoint point = [touch locationInView:self.cameraView];
point.x /= self.cameraView.frame.size.width;
point.y /= self.cameraView.frame.size.height;
[self focusAtPoint:point];
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Marco Gasparetto
  • 463
  • 5
  • 10