4

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

lab12
  • 6,400
  • 21
  • 68
  • 106
  • You also have to add NSPhotoLibraryUsageDescription to info.plist: http://stackoverflow.com/questions/39519773/nsphotolibraryusagedescription-key-must-be-present-in-info-plist-to-use-camera-r – Marcin Mierzejewski Dec 21 '16 at 12:04

3 Answers3

6

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]);
        }
    }];
Gokul
  • 1,236
  • 1
  • 14
  • 26
3

Use the UISaveVideoAtPathToSavedPhotosAlbum function.

Kris Markel
  • 12,142
  • 3
  • 43
  • 40
1

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");
            }

        }];
    });
});
DJ1
  • 936
  • 15
  • 29