Amit Mandloi
Amit Mandloi

Reputation: 13

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",

Upvotes: 0

Views: 1597

Answers (2)

TheHungryCub
TheHungryCub

Reputation: 1970

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

});
}

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

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.

Upvotes: 0

Related Questions