views:

169

answers:

0

Hey.

What I want to do is to always use the same instance of UIImagePickerController in my whole app, since i don't want to alloc and destroy my picker every time the user takes a picture. The app uses a picker and an UIImageView named pictureA to show the picture choossen from the iPhone album or taken with the camera.

At start I allock my picker view controller like this:

    imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    [self addSubview:imagePickerController.view];

When I take picture from the picker and place it in my UIImageView:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    // newImage is a UIImage do not try to use a UIImageView    
    UIImage *newImage = [info objectForKey:@"UIImagePickerControllerEditedImage"];

        [pictureA removeFromSuperview];

        CGRect myImageRect = CGRectMake(30.0f, 38.0f, 125.0f, 125.0f); 
        pictureA = [[UIImageView alloc] initWithFrame:myImageRect];
        pictureA.contentMode= UIViewContentModeScaleAspectFit;
        [pictureA setImage:newImage];
        pictureA.opaque = YES;
        [self addSubview:pictureA];


        [newImage release];

        [picker dismissModalViewControllerAnimated:YES];
        [picker.view setHidden:YES];

    }

When chooses a picture from the iphone's library:

- (void) chooseImageFromLibrary {
    [imagePickerController.view setHidden:NO];
        imagePickerController.allowsEditing = YES;
    [imagePickerController viewWillAppear:YES];
    [imagePickerController viewDidAppear:YES];
}

Cancel:

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

- (void) takePicture {


    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    {
        [(MainScene*)[melonGame actualScene] setCameraDissabled:YES];
        return;
    }


    [imagePickerController.view setHidden:NO];

    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 
    imagePickerController.allowsEditing = YES;

    [imagePickerController takePicture];


    [imagePickerController viewWillAppear:YES];
    [imagePickerController viewDidAppear:YES];
}

The app will work properly when i take the first picture, but when i try to take another picture or choose another picture from my library it looks exactly as the last time i selected a picture ( with the picture in all the screen and an inactive menu to choose and cancel at the bottom). My app only works correctly if i remove the controller from my view and alloc it every time the user takes a picture. Is there any way to "clean" or "restart" the picker without releasing it and removing it from my view ?

I'm also not sure if i can instance once the UIImageView ( pictureA) where I store the image taken by the imagePickerController and modify it every time the user takes a picture or chooses one from the album, i suspect that it's incorrect to destroy it and realloc it every time the user takes a picture. Any suggestions ?, any thing i'm doing wrong regarding memory management or the use of the UIImagePickerController and the UIImageView ?

thanks !