网络:NSURLSession 下载文件
2016-04-20 20:12
477 查看
#import "ViewController.h" #import "SSZipArchive.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } // 使用NSURLSession 下载,内存锋值不高。但是有些Xcode版本,峰值很6.3以前 // 但是在真机上不存在这个问题 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // NSURL NSURL *url = [NSURL URLWithString:@"http://localhost/ui.mp4"]; // Session发起请求 // location 文件下载完这后保存的路径 [[[NSURLSession sharedSession]downloadTaskWithURL:url completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"%@ -- %@",location.path, response); // 下载完文件之后,文件被删除了 // 因为系统以为你下载的是压缩包,下载完成之后需要解压。解压完成之后,删除原文件 // 解压 /* 1. 压缩包的路径 2. 解压到的路径 */ // [SSZipArchive unzipFileAtPath:location.path toDestination:[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject]]; // 下载视频,需要解压吗 不需要 // 复制一份到另一个目录 NSFileManager *manager = [NSFileManager defaultManager]; // 保存文件的路径 NSString *fileName = [url.path lastPathComponent]; NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:fileName]; // 把临时文件移动到缓存目录,并且改成我们需要的后缀名 NSLog(@"%@",path); // 使用复制或者移动都可以 // [manager moveItemAtPath:location.path toPath:path error:NULL]; [manager copyItemAtPath:location.path toPath:path error:NULL]; }] resume]; } @end
相关文章推荐
- 2016西电校赛网络赛 Problem E 删除字符
- Android基于http的网络请求async-http框架
- 基于Windows的迭代回声服务器端/客户端(基于TCP的服务器端/客户端)
- 2016西电校赛网络赛 Problem D 抢人头
- 2016西电校赛网络赛 Problem C 寻找万神
- 2016西电校赛网络赛Problem B 猴子吃桃 II
- 2016西电校赛网络赛 Problem A z1+z2
- HTML5第7次课堂笔记(图片验证码的应用,xmlhttprequest对象的使用,跨域)
- 网络:NSURLSession
- HTTP笔记
- bzoj4538: [Hnoi2016]网络
- 解决 nginx https反向代理http协议 302重定向localtion到http问题
- linux 网络编程基础(四)read,write,connect, accept 超时封装
- IP地址、子网掩码、网络号、主机号、网络地址、主机地址
- android开发网络请求
- Android开发请求网络方式
- [BZOJ4538]网络
- linux 网络编程基础(三)selsct函数应用
- Cocos2d-x3.1TestCpp之NewRenderTest Demo分析
- http、TCP/IP协议与socket之间的区别