iOS:从网址下载视频并保存在图库......?

时间:2016-04-20 06:36:07

标签: ios iphone quickblox

我无法弄清楚如何从网址下载视频并将其保存到图库中。

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无法保存到已保存的相册:错误域= NSURLErrorDomain代码= -1100“此服务器上找不到请求的网址。” UserInfo = 0x7f9c13ccb130 {NSUnderlyingError = 0x7f9c13cc3aa0“操作无法完成。没有这样的文件或目录”,

2 个答案:

答案 0 :(得分:0)

来自Apple's documentation我认为UIVideoAtPathIsCompatibleWithSavedPhotosAlbum仅适用于本地文件,而不适用于远程文件。 UISaveVideoAtPathToSavedPhotosAlbum也是如此。

您需要先将文件下载到设备,然后才能使用UIVideoAtPathIsCompatibleWithSavedPhotosAlbum。我建议使用AFNetworking之类的东西来做这件事。

答案 1 :(得分:0)

使用此代码。我希望它能正常运行。:

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

});
}