繁体   English   中英

iOS:从url下载视频并保存在图库中吗?

[英]iOS: Download video from url and save in gallery…?

我不知道如何从url下载视频并将其保存到图库。

BOOL compatible = UIVideoAtPathIsCompatibleWithSavedPhotosAlbum([videoURL path]);
        // save
        NSLog(@"BOOL compatible....%hhd",compatible);

        if (compatible){
            UISaveVideoAtPathToSavedPhotosAlbum([videoURL path], nil, nil, nil);
            NSLog(@"SAVED!!!! %@",[videoURL path]);
        }else
        {
            NSLog(@"INCOMPATIBLE...");
        }

当下载显示错误的视频时: https ://api.quickblox.com/blobs/4185382/download无法保存到保存的相册中:错误Domain = NSURLErrorDomain代码= -1100“在此服务器上找不到请求的URL。” UserInfo = 0x7f9c13ccb130 {NSUnderlyingError = 0x7f9c13cc3aa0“无法完成操作。没有这样的文件或目录”,

苹果的文档中,我认为UIVideoAtPathIsCompatibleWithSavedPhotosAlbum仅适用于本地文件,而不适用于远程文件。 UISaveVideoAtPathToSavedPhotosAlbum也是如此。

您必须先将文件下载到设备上,然后才能使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 我建议使用类似AFNetworking的方法。

使用此代码,我希望它能正常工作。

    -(void)DownloadVideo
{
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Downloading Started");
NSString *urlToDownload = @"http://www.somewhere.com/thefile.mp4";
NSURL  *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
    {
    NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString  *documentsDirectory = [paths objectAtIndex:0];

    NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];

    //saving is done on main thread
    dispatch_async(dispatch_get_main_queue(), ^{
        [urlData writeToFile:filePath atomically:YES];
        NSLog(@"File Saved !");
    });
    }

});
}

暂无
暂无

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

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