Save a Video that I record in my app

10,803

Try this:

UISaveVideoAtPathToSavedPhotosAlbum(moviepath,nil,nil,nil);

Edit: Try this and modify your code to this method:

// For responding to the user tapping Cancel.
- (void) imagePickerControllerDidCancel: (UIImagePickerController *) picker {

    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}

// For responding to the user accepting a newly-captured picture or movie
- (void) imagePickerController: (UIImagePickerController *) picker
            didFinishPickingMediaWithInfo: (NSDictionary *) info {

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToSave;

    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
            == kCFCompareEqualTo) {

        editedImage = (UIImage *) [info objectForKey:
                    UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:
                    UIImagePickerControllerOriginalImage];

        if (editedImage) {
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }

    // Save the new image (original or edited) to the Camera Roll
        UIImageWriteToSavedPhotosAlbum (imageToSave, nil, nil , nil);
    }

    // Handle a movie capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
            == kCFCompareEqualTo) {

        NSString *moviePath = [[info objectForKey:
                    UIImagePickerControllerMediaURL] path];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (
                    moviePath, nil, nil, nil);
        }
    }

    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}
Share:
10,803
jwknz
Author by

jwknz

Hi, Working as a CS Tutor and enjoying full stack development in my day-to-day coding. Cheers, Jeff

Updated on July 30, 2022

Comments

  • jwknz
    jwknz over 1 year

    I am succeeding in understanding how to make a camera app in my learning journey:-)

    The only thing I am stuck with is saving a video that I have recorded. I am able to save a photo, but the same does not work for videos.

    So I think I have almost got it with the help of iBrad Apps.

    got this code:

    -(void)imagePickerController:(UIImagePickerController *)picker
       didFinishPickingMediaWithInfo:(NSDictionary *)info
     {
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    
    [self dismissModalViewControllerAnimated:YES];
    
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        UIImage *image = [info 
                          objectForKey:UIImagePickerControllerOriginalImage];
    
        imageView.image = image;
    
        if (newMedia)
            UIImageWriteToSavedPhotosAlbum(image, 
                                           self,
                                           @selector(image:finishedSavingWithError:contextInfo:),
                                           nil);
    }
    else{
    
        if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
            UIImage *movie = [info 
                              objectForKey:UIImagePickerControllerQualityTypeHigh];
    
            videoRecorder2.image = movie;
    
                if (newMedia)
                    UISaveVideoAtPathToSavedPhotosAlbum(movie, 
                                                   self,
                                                   @selector(movie:finishedSavingWithError:contextInfo:),
                                                   nil);
    }}}
    

    I have an if statement because the app can take both video and still images.

    The first part is for still - which works and then the second part I am still tutu-ing with:-)

  • jwknz
    jwknz over 12 years
    Sounds like I the right way - I have added some code to show what I am doing - cheers
  • SimplyKiwi
    SimplyKiwi over 12 years
    Check if movie is nil or if newMedia is nil.
  • jwknz
    jwknz over 12 years
    Sorry is that a question or the answer? newMedia is a Boolean and so I am thinking that it should be nil - so that it can save the clip right? maybe I am on the wrong trail here.
  • SimplyKiwi
    SimplyKiwi over 12 years
    Check that both of those objects exist then I we will work from there.
  • jwknz
    jwknz over 12 years
    IBOutlet UIImageView *videoRecorder2; BOOL newMedia; There they are in my .h file - they come up the green-ish color so it is recognizing them.
  • SimplyKiwi
    SimplyKiwi over 12 years
    use an NSLog and check if they exist.
  • jwknz
    jwknz over 12 years
    ok - mmmm maybe bad code practice, but I have never used NSLog before. sorry:-(
  • SimplyKiwi
    SimplyKiwi over 12 years
    try removing this line: videoRecorder2.image = movie;
  • jwknz
    jwknz over 12 years
    I am getting this warning: Incompatible pointer types passing 'UIImage *' to parameter of type 'NSString *'
  • jwknz
    jwknz over 12 years
    ok cool :-) It seems to work, I still got a few bugs, but i'll learn and work through them - thanks iBrad:-)
  • SimplyKiwi
    SimplyKiwi over 12 years
    no problem! Yea your main issue before was that you were setting a Image to a Video. haha :)
  • jwknz
    jwknz over 12 years
    ok - yeah that makes sense, when I tried your code it would not dismiss the view though, neither images or video - so I combined my initial code and after the else entered your video part of the solution and it works after that. I'll do some more staring at the screen and see if I can make it work cleaner:-)
  • Lior Frenkel
    Lior Frenkel over 12 years
    Thanks! How can I later load up this video from the saved place to the GUI?
  • SimplyKiwi
    SimplyKiwi almost 12 years
    Sorry for the really late response. Just use MPMoviePlayerController and do it that way since it is the easiest way. :P