Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.