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

Get problem when try to save the image

when I try to redo the code for the self-destruction message app, I get problem when try to realize the function to store the image. If I pick the image in the photo album, the application will crash. Following is my code:

(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString * mediaType = [info objectForKey:UIImagePickerControllerOriginalImage]; 
  if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { 
      //image is selected
        self.image=[info objectForKey:UIImagePickerControllerOriginalImage];
        if(self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera){
            UIImageWriteToSavedPhotosAlbum(self.image, nil, nil, nil);
        }
    }
    else{
        //video is selected
        NSURL * url=[info objectForKey:UIImagePickerControllerMediaURL];
        self.videoPath =[url path];
        if(self.imagePicker.sourceType == UIImagePickerControllerSourceTypeCamera){
            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.videoPath)) {
                UISaveVideoAtPathToSavedPhotosAlbum(self.videoPath, nil, nil, nil);
            }  
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

When I debug the function, I find when the NSString type variable mediaType is used in the if statement: if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) , the debug window shows that the type of mediatype is UIImage.

Stone Preston
Stone Preston
42,016 Points

to format code oyu need to use 3 backticks ( ` ) not three single quotes ( ' ). the backtick is located below the escape key on my keyboard. The same key as the ~

1 Answer

Stone Preston
Stone Preston
42,016 Points

here is your error:

NSString * mediaType = [info objectForKey:UIImagePickerControllerOriginalImage];

needs to be

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

Thanks again, Stone. BTW, do u know how to format my code in the discussion board? I try the way: ''' code ''' But it doesn't work.

Oh, u just anser my question. Thanks very much!

Oh, u just anser my question. Thanks very much!