Saving a Video to the Photo Library - iPhone SDK

后端 未结 3 1541
情深已故
情深已故 2020-12-18 08:31

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

相关标签:
3条回答
  • 2020-12-18 09:23

    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");
                }
    
            }];
        });
    });
    
    0 讨论(0)
  • 2020-12-18 09:27

    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]);
            }
        }];
    
    0 讨论(0)
  • 2020-12-18 09:34

    Use the UISaveVideoAtPathToSavedPhotosAlbum function.

    0 讨论(0)
提交回复
热议问题