Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Self-Destructing Message iPhone App Capturing Photo and Video Using UIImagePickerController Saving the Media

UIImagePicker briefly disappears and then returns again. It seems like viewDidAppear is being called again?

Every time the UIImagePicker is dismissed via method [self dismissViewControllerAnimated: YES completion: nil]; the image picker reappears instantly barely showing the tableViewController underneath the stack. I believe it has something to do with the viewWillAppear being called again after the view controller is dismissed but I don't know where to put the imagePicker then because viewDidLoad will not create the camera every time it is needed.. please help!

Here are the viewWillAppear and didFinishPickingMediaInfo methods:

  • (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated];

    self.imagePicker = [[UIImagePickerController alloc] init]; self.imagePicker.delegate = self; self.imagePicker.allowsEditing = NO; self.imagePicker.videoMaximumDuration = 10;

    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){ self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; }else { self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; }

    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];

    [self presentViewController:self.imagePicker animated:NO completion:nil];

}

//....

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

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {

    self.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    if (self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera){
    
        UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
        }
    

    } else { NSURL *imagePickerURL = [info objectForKey:UIImagePickerControllerMediaURL]; self.videoFilePath = [imagePickerURL path];

    if(UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoFilePath)){
    
    UISaveVideoAtPathToSavedPhotosAlbum(self.videoFilePath, nil, nil, nil);
    }
    

[self dismissViewControllerAnimated: YES completion: nil]; }
}

1 Answer

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Hi Cullen,

To bypass UIImagePicker being shown again when viewWillAppear: is recalled you can use a condition similar to this:

if (self.image == nil && [self.videoFilePath length] == 0) {
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.videoMaximumDuration = 10;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    } else {
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    self.imagePicker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:self.imagePicker.sourceType];
    [self presentViewController:self.imagePicker animated:NO completion:nil];
}

This will ensure that the picker controller won't be called again if you have already selected an image or a video file.