简体   繁体   中英

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

I can't figure out how to download video from url and save it to the gallery.

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

when download video showing error: https://api.quickblox.com/blobs/4185382/download cannot be saved to the saved photos album: Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo=0x7f9c13ccb130 {NSUnderlyingError=0x7f9c13cc3aa0 "The operation couldn't be completed. No such file or directory",

From Apple's documentation I think that UIVideoAtPathIsCompatibleWithSavedPhotosAlbum only works on local files, not remote ones. The same will be true for UISaveVideoAtPathToSavedPhotosAlbum .

You will need to download the file to your device first, before you can use UIVideoAtPathIsCompatibleWithSavedPhotosAlbum . I would recommend using something like AFNetworking to do that.

Use this code.I hope it will work properly.:

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

});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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