如何下载一个视频文件到Documents目录下的Video文件夹
2015-12-26 17:30
791 查看
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //获取程序根目录 // NSString *rootPath = NSHomeDirectory(); //获取程序根目录下得Ducuments // NSString *documentsPath = [rootPath stringByAppendingFormat:@"/%@",@"Documents"]; //或者 // documentsPath = [rootPath stringByAppendingPathComponent:@"Documents"]; // 常用的获取Documents目录方法 // NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; // NSLog(@"documentsPath = %@",documentsPath); //下载一个视频文件到Documents目录下得Video文件夹 NSString *videoPath = [self creatDirInDocuments:@"Video"]; NSLog(@"videoPath = %@",videoPath); if (videoPath !=nil) { NSFileManager *fileManage = [NSFileManager defaultManager]; NSString *videoUrlString = @"http://v8.tv.cctv5.cctv.com/r5wbth/4d/e7/4de76971-63f4-4717-f28b-03d757a7704f/mp4h.mp4"; NSRange range = [[videoUrlString lastPathComponent]rangeOfString:@".mp4"];// NSString *videoUrlStirngNew = [[videoUrlString lastPathComponent]substringFromIndex:range.location+4]; NSString *fileVideo = [videoPath stringByAppendingPathComponent:videoUrlStirngNew]; //如果文件夹不存在该路径 if (![fileManage fileExistsAtPath:fileVideo]) { //进行编码 // videoUrlString = [videoUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSCharacterSet *characterSet = [NSCharacterSet URLQueryAllowedCharacterSet]; videoUrlString = [videoUrlString stringByAddingPercentEncodingWithAllowedCharacters:characterSet]; //我们的图片 视频 音频等在网络中都是以二进制文件传输,所以我们这里拿到的是data NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:videoUrlString]]; if (data == nil) { NSLog(@"网络出错,请稍后再试"); } else { //用单例类 NSFileManager的对象,将文件写入本地 BOOL isSuccess = [fileManage createFileAtPath:fileVideo contents:data attributes:nil]; if (isSuccess) { NSLog(@"视频下载成功"); } else { NSLog(@"视频下载失败"); } } } } } //第一步封装 //这里我们封装一个函数,使得这个函数返回的是我们在Documents目录下想要的文件夹的路径 -(NSString *)creatDirInDocuments:(NSString *)dirName { //获得Documents的文件路径 NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]; //拼接成我们想要的文件的路径的字符串 NSString *dirDocuments = [documentsPath stringByAppendingPathComponent:dirName]; //获取NSFileManager 单例类,用文件操作 NSFileManager *fileManager = [NSFileManager defaultManager]; //判断是否存在某个文件或文件夹 BOOL isExist = [fileManager fileExistsAtPath:dirDocuments]; if (!isExist) { //创建文件夹 NSError *error; BOOL isSuccess = [fileManager createDirectoryAtPath:dirDocuments withIntermediateDirectories:YES attributes:nil error:&error]; if (!isSuccess) { //如果文件夹创建失败,将打印错误信息 NSLog(@"error = %@",error.debugDescription); dirDocuments = nil; } } return dirDocuments; }
相关文章推荐
- linux系统中OpecnCV编译
- 交换排序之高速排序
- 常用数据库语句
- UVa 10935 - Throwing cards away I
- openstack之ironic分析
- Activity的四种启动模式和onNewIntent()
- 深入理解Android异步消息处理机制
- JavaScript
- 17.5 设置段落格式
- Android 之一个很好的Viewpager滑动指示器
- C++学习:类成员的访问范围
- jquery 仿文本编辑器(智能提示输入文字)
- .net发邮件
- UITableView 上添加button
- Android开发:15分钟学会Java正则表达式
- Swift 数组
- python数据可视化--散点图以及分类
- OpenStack-RPC-server的构建(三)
- Head first笔记二:观察者模式
- linux下自动检测服务端口是否正常监听。