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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3851294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:48:43  来源:igfitidea点击:

Saving a Video to the Photo Library - iPhone SDK

iphoneobjective-cxcodevideophoto

提问by lab12

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.

有什么办法可以将 Documents 目录中的视频保存到照片库中吗?我在文档目录中有视频的链接,我只是不知道如何将它保存到照片应用程序中。

Thanks,

谢谢,

Kevin

凯文

采纳答案by Kris Markel

回答by Gokul

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

回答by DJ1

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

        }];
    });
});