Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with UIImagePickerController cancel button not working

I have an universal app that allows to select an image from the device photo library for later manipulation, the code works fine on the iPad but nothing happens on the iPhone, not even the cancel button and after an image is selected nothing happens neither here is my code:

-(IBAction)grabImage:(id)sender
{
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    imgPicker = [[UIImagePickerController alloc] init];
    [imgPicker setDelegate:self];

    popover = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
    [popover setDelegate:self];

    CGPoint position        = [view1.superview convertPoint:view1.frame.origin toView:nil];
    CGRect popOverFrame     = CGRectMake( position.x, position.y, self.view.frame.size.width, self.view.frame.size.height );

    [popover presentPopoverFromRect:popOverFrame inView:self.view permittedArrowDirections:nil animated:NO];
    [popover setPopoverContentSize:CGSizeMake(320, 480)];
    [imgPicker release];
}
else
{
    imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentModalViewController:self.imgPicker animated:YES];
    [imgPicker release];
}
}


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];

CGImageRef imgRef = pickedImage.CGImage;

    app->setImage( pickedImage, CGImageGetWidth(imgRef), CGImageGetHeight(imgRef) );

    [[picker parentViewController] dismissModalViewControllerAnimated:YES];

// Enable texture siwth after an image has been loaded
[textureSwitch setEnabled:YES];
[textureSwitch setOn:YES];
app->isTextureDrawingOn     = [textureSwitch isOn];

[fillsSwitch setOn:NO];
app->isFillsDrawingOn       = [fillsSwitch isOn];

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
    [popover dismissPopoverAnimated:YES];
}
ofLog(OF_LOG_VERBOSE, "cancel after selection");
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];

if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
{
        [popover dismissPopoverAnimated:YES];
    }

ofLog(OF_LOG_VERBOSE, "did cancel");
}
like image 789
Ricardo Sanchez Avatar asked Apr 21 '12 17:04

Ricardo Sanchez


1 Answers

Instead of using below code.

[[picker parentViewController] dismissModalViewControllerAnimated:YES];

Try this code

[self dismissModalViewControllerAnimated:YES];

and also check did you add UIImagePickerControllerDelegate in your interface file.

SOLUTION: (From my comment)

Try this [self.imgPicker dismissModalViewControllerAnimated:YES]; This will work.

For iOS 7: To dismiss a present view controller

[self.imgPicker dismissViewControllerAnimated: YES completion: NULL];
like image 66
Dinesh Raja Avatar answered Nov 09 '22 23:11

Dinesh Raja