Saving a Video to the Photo Library - iPhone SDK

12,633

Solution 1

Use the UISaveVideoAtPathToSavedPhotosAlbum function.

Solution 2

If you are saving it in a local directory first, then you can save it as..

    ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
    [assetLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
        if(error) {
           NSLog(@"error while saving to camera roll %@",[error localizedDescription]);        
        } else {
            //For removing the back up copy from the documents directory           
            NSError *removeError = nil;
            [[NSFileManager defaultManager] removeItemAtURL:url error:&removeError];
            NSLog(@"%@",[removeError localizedDescription]);
        }
    }];

Solution 3

Try this

You can also use this code to download and save video from internet to Photos.

NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url or Path"]];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{

    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];

    dispatch_async(dispatch_get_main_queue(), ^{

        // Write it to cache directory
        NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
       [videoData writeToFile:videoPath atomically:YES];

       // After that use this path to save it to PhotoLibrary
       ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
       [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
        {
            if (error)
            {
                NSLog("Error");
            }
            else
            {
                NSLog("Success");
            }

        }];
    });
});
Share:
12,633
lab12
Author by

lab12

Updated on June 25, 2022

Comments

  • lab12
    lab12 almost 2 years

    Is there any way for me to save a video in the Documents directory to the Photos Library? I have the link of the video in the documents directory, I just don't know how to save it to the Photos app.

    Thanks,

    Kevin

  • zszen
    zszen almost 6 years
    deprecated in ios9+