Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using UIImagePickerController in UIView

I have button which when I click displays the camera (works perfectly)

- (IBAction)getPhoto:(id)sender
{
    NSLog( @"Button clicked!" );
    imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
    }

    else {  // IF the device doesn't have a camera, so use the photos album
        [imagePicker setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }

    [self addSubview:imagePicker.view];
}

After which I can take an image, etc, - But when I click "use" : nothing happens here is my method : the Image picker does not dismiss itslelf : the application simply does nothing, I'm trying to take the image I just took and display it inside a CGRect on screen .

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:NULL];

    UIImageView *patientImage = [[UIImageView alloc] init];
    patientImage.frame = CGRectMake(625, 25, 83, 103);

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.patientImage.image = chosenImage;
}

Even when I hit the cancel button without taking an image, the imagePicker (camera interface) does not dismiss itself

Any help, advice, or guidance would be very appreciated.

like image 443
Taskinul Haque Avatar asked Jun 21 '13 09:06

Taskinul Haque


3 Answers

You shouldn't be doing the "addSubview" thing up there but instead do:

[self presentViewController:imagePicker animated:YES completion:nil];
like image 50
Michael Dautermann Avatar answered Sep 19 '22 21:09

Michael Dautermann


Instead of adding the picker as a subview, you should add the picker to a UIPopoverController.

You can then display it from within your UIView subclass by using the presentPopoverFromRect:inView:permittedArrowDirections:animated: popover method like this:

self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
[self.popover presentPopoverFromRect:sender.frame inView:self permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];

Then, you can use the delegate methods to dismiss the popover using:

[self.popover dismissPopoverAnimatedYES];

The is required by the official docs:

The table indicates that on iPad, if you specify a source type of UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum, you must present the image picker using a popover controller, as described in “Presenting and Dismissing the Popover” in UIPopoverController Class Reference. If you attempt to present an image picker modally (full-screen) for choosing among saved pictures and movies, the system raises an exception.

On iPad, if you specify a source type of UIImagePickerControllerSourceTypeCamera, you can present the image picker modally (full-screen) or by using a popover. However, Apple recommends that you present the camera interface only full-screen.

like image 38
lnafziger Avatar answered Sep 19 '22 21:09

lnafziger


Is the getPhoto: method in the UIView subclass?

If thats the case, then it would be better to:

  1. Move the getPhoto: code out of the UIView subclass entirely, and put it in the view's parent view controller.

  2. Make your UIButton a property of your UIView: @property (nonatomic, strong) UIButton *myButton;

  3. In your view controller, set the button's target & action as follows:

    [myView.myButton addTarget:self action:@selector(getPhoto:) forControlEvents:UIControlEventTouchUpInside];
    
  4. Finally, in your getPhoto: method, call [self presentViewController:imagePicker animated:YES completion:NULL];

  5. Your - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info code should also go in the view controller, changing self.patientImage.image = chosenImage; to myView.patientImage.image = chosenImage;

like image 29
Vinny Coyne Avatar answered Sep 18 '22 21:09

Vinny Coyne