简体   繁体   English

将视频保存到照片库 - iPhone SDK

[英]Saving a Video to the Photo Library - iPhone SDK

Is there any way for me to save a video in the Documents directory to the Photos Library? 有什么办法可以将Documents目录中的视频保存到照片库吗? I have the link of the video in the documents directory, I just don't know how to save it to the Photos app. 我在文档目录中有视频的链接,我只是不知道如何将其保存到Photos应用程序。

Thanks, 谢谢,

Kevin 凯文

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

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

        }];
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM