【问题标题】:iOS: Download video from url and save in gallery......?iOS:从网址下载视频并保存在图库中......?
【发布时间】:2016-04-20 06:36:07
【问题描述】:

我不知道如何从 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 无法保存到已保存的相册中:错误域=NSURLErrorDomain Code=-1100 "在此服务器上找不到请求的 URL。" UserInfo=0x7f9c13ccb130 {NSUnderlyingError=0x7f9c13cc3aa0 "操作无法完成。没有这样的文件或目录",

【问题讨论】:

  • 你的视频网址不正确。
  • 我不明白哪个视频网址不正确。
  • @KlimczakM 如何视频网址不正确 - 只是告诉别人他们错了并不能帮助他们解决问题!
  • 问题中的URL当然是正确的。我的意思是代码中传递的 URL 可能不正确。更多会有所帮助。

标签: ios iphone quickblox


【解决方案1】:

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

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

});
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 2011-01-30
      相关资源
      最近更新 更多