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

前端 未结 4 1871
旧巷少年郎
旧巷少年郎 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: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");
                }
            }
        });
    }
    

提交回复
热议问题