简体   繁体   中英

IOS development, using UIImagePickerController and pressing use make the app get stuck (not crash)

Can any one help me with this problem?

As in my last question, I am using a tabBarController with 3 tab items. The 3rd tab is has a uiViewController with a UIImagePickerController in it(a camera).

now every things is working except from one thing. When take an image with the camera and pressing "use" i am getting the alert the the image was save and i can see it in the photo album (if i close the app and look at it) but the app get stuck at this poing and i can not do anything any more. I can see the image on the screen and the "use" and "retake" buttons are not usable. just stuck like that.

Can any one see what am i doing wrong over here?

ps. In all of the examples and tutorials i found there is a release of the picker in the cancel...(also in my code). The picker in my case is a property of the view controller (imgPicker) and i release it as always in the dealloc method, Is that write or wrong? should i live it like that or am i doing a bad memory thing over here (I am not getting any "bad memory error" over here but it might be my mistake...)?

I load the UIImagePicker in the viveWillAppear delegate method. Every thing is in the same TakePhotoViewController.m file...

-(void) viewWillAppear:(BOOL)animated{
    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsEditing = NO;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self presentModalViewController:imgPicker animated:YES];  
}

and the delegate methods:

#pragma mark -
#pragma mark - UIImagePicker delegate methods

//saving the image that was taken
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
    // Access the uncropped image from info dictionary
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    // Save image
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    [picker release];
}

//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    UIAlertView *alert;

    // Unable to save the image  
    if (error)
        alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                       message:@"Unable to save image to Photo Album." 
                                      delegate:self cancelButtonTitle:@"Ok" 
                             otherButtonTitles:nil];
    else // All is well
        alert = [[UIAlertView alloc] initWithTitle:@"Success" 
                                       message:@"Image saved to Photo Album." 
                                      delegate:self cancelButtonTitle:@"Ok" 
                             otherButtonTitles:nil];
    [alert show];
    [alert release];
}

//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
    [self.tabBarController setSelectedIndex:0];
}

Thank you very much, Erez

You're on the right track with the Cancel Action, but failed to do it for the other actions:

You need to call dismiss on ViewController after each action.

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html

EDIT: New link https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/doc/uid/TP40007070

... However, if you set this property to YES, your delegate must dismiss the image picker interface after the user takes one picture or cancels the operation.

So, if you need to take more than 1 picture, showCameraControls needs to be set to NO, and you need to use your own CameraOverlayView.

Also, definitely DO NOT release picker in your delegate! When the ViewController pops the view, it will release it automatically (Assuming that the picker is not being retained elsewhere). Rule of thumb, if you don't own (retain) it, don't release it. (This concept will probably be forgotten once iOS5 ARC is commonplace)

You should probably release the ViewController after it has been presented.

iPhone - modalViewController release

EDIT: more code to help

-(void) viewWillAppear:(BOOL)animated{
    // No need to store... this is 1 use only anyway. Save memory, and release it when done.
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.allowsEditing = NO;
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:imgPicker animated:YES];
    [imgPicker release]; // Release this here, this will execute when modal view is popped. 
}

Delegate:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo: (NSDictionary *)info
{
    /* Do what you need to do then */
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

//alerting the user if the images was saved or not
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    /* Do what you need to do then */
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

//if user is cancelling the camera
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    /* Do what you need to do then */
    [[picker parentViewController] dismissModalViewControllerAnimated:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM