How to download video from url and save it in to document directory in iOS?

前端 未结 4 1840
旧巷少年郎
旧巷少年郎 2021-01-03 12:57

How to download video from url and save it in to document directory in iOS

相关标签:
4条回答
  • 2021-01-03 13:13

    You can also implement same in Swift 2.0

    class func downloadVideo(videoImageUrl:String)
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            //All stuff here
    
            let url=NSURL(string: videoImageUrl)
            let urlData=NSData(contentsOfURL: url!)
    
            if((urlData) != nil)
            {
                let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
    
                let fileName = videoImageUrl.lastPathComponent //.stringByDeletingPathExtension
    
                let filePath="\(documentsPath)/\(fileName)"
    
                //saving is done on main thread
    
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
    
                     urlData?.writeToFile(filePath, atomically: true)
                })
    
            }
        })
    
    }
    
    0 讨论(0)
  • 2021-01-03 13:15

    You can download it using GCD.

    -(void)downloadVideoAndSave :(NSString*)videoUrl
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    
            NSData *yourVideoData=[NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrl]];
    
            if (yourVideoData) {
                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDirectory = [paths objectAtIndex:0];
    
                NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"video.mp4"];
    
                if([yourVideoData writeToFile:videpPath atomically:YES])
                {
                    NSLog(@"write successfull");
                }
                else{
                    NSLog(@"write failed");
                }
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-03 13:18

    You can use the NSURLConnection method sendAsynchronousRequest:queue:completionHandler:.

    That one method will download an entire file into an NSData object and call your completion handler method once it's done.

    If you can write a app that requires iOS 7 or later, you could also use the new NSURLSession API. That offers a lot more features.

    If you search using those terms you should be able to find tutorials and sample apps illustrating both APIs.

    0 讨论(0)
  • 2021-01-03 13:23

    use this code it is working in my current project

    -(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 !");
        });
        }
    
    });
    }
    
    0 讨论(0)
提交回复
热议问题