TotoroTotoro
TotoroTotoro

Reputation: 17612

Non-modal UIImagePickerController, tap to focus doesn't work

I'm using a UIImagePickerController in a non-modal setting, like this:

UIImagePickerController *_imagePickerVC = // init the VC with the main camera as the source
[_mainCameraPreviewContainer addSubview:_imagePickerVC.view];
[_mainCameraPreviewContainer bringSubviewToFront:_imagePickerVC.view];

This works, and I can take pictures, but I can't tap on the preview area to make the camera focus on that point. I tried fixing this by adding one or both of these 2 lines:

_imagePickerVC.showsCameraControls = NO;
_imagePickerVC.view.userInteractionEnabled = YES;

but no luck. When I make it show the camera controls, I do get the flash mode button and the button to choose the camera, but those buttons are not tappable as well. Is it possible to make the tap to focus work in my situation?

Upvotes: 4

Views: 2121

Answers (3)

Arun
Arun

Reputation: 590

Try this and let me know..

_imagePickerVC.view.userInteractionEnabled = YES;
UIPanGestureRecognizer *pgr = [[UITapGestureRecognizer alloc] 
                               initWithTarget:self action:@selector(handleTap:)];

[_imagePickerVC addGestureRecognizer:pgr];
[pgr release];


-(void)handleTap{
    AVCaptureDevice *device = [[self captureInput] device];

    NSError *error;

     if ([device isFocusModeSupported:AVCaptureFocusModeAutoFocus] &&
         [device isFocusPointOfInterestSupported])
     {
         if ([device lockForConfiguration:&error]) {
             [device setFocusPointOfInterest:point];

        CGPoint point = [touch locationInView:self.cameraView];
    point.x /= self.cameraView.frame.size.width;
    point.y /= self.cameraView.frame.size.height;
    [self focusAtPoint:point];

             [device unlockForConfiguration];
         } else {
             NSLog(@"Error: %@", error);
         }
     }
}

Upvotes: 1

Sergey Kuryanov
Sergey Kuryanov

Reputation: 6114

I repeated your code and tap to focus and buttons works. (iOS 5.1, 6.1).

- (IBAction)buttonTap:(id)sender {
    if (!_imagePickerVC)
    {
        _imagePickerVC = [UIImagePickerController new];
        _imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self.view addSubview:_imagePickerVC.view];
    [self.view bringSubviewToFront:_imagePickerVC.view];
}

Ensure that mainCameraPreviewContainer.userInteractionEnabled == YES.

Upvotes: 3

Pushpak Narasimhan
Pushpak Narasimhan

Reputation: 454

http://jcuz.wordpress.com/2010/02/17/pickerfocus/ You can give this a try.

I would suggest you to implement using AVfoundation. Its more easy and more flexible. using AVFoundation ios AVFoundation tap to focus

Upvotes: 1

Related Questions