IOS-网络(大文件下载)
生活随笔
收集整理的這篇文章主要介紹了
IOS-网络(大文件下载)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、不合理方式
1 // 2 // ViewController.m 3 // IOS_0131_大文件下載 4 // 5 // Created by ma c on 16/1/31. 6 // Copyright ? 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController ()<NSURLConnectionDataDelegate> 12 13 //進(jìn)度條 14 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 15 //存數(shù)據(jù) 16 @property (nonatomic, strong) NSMutableData *fileData; 17 //文件總長度 18 @property (nonatomic, assign) long long totalLength; 19 20 @end 21 22 @implementation ViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 self.view.backgroundColor = [UIColor cyanColor]; 28 } 29 30 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 31 { 32 [self download]; 33 } 34 35 - (void)download 36 { 37 //1.NSURL 38 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/minion_01.mp4"]; 39 //2.請求 40 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 41 //3.下載(創(chuàng)建完conn后會自動發(fā)起一個異步請求) 42 [NSURLConnection connectionWithRequest:request delegate:self]; 43 44 //[[NSURLConnection alloc] initWithRequest:request delegate:self]; 45 //[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 46 //NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; 47 //[conn start]; 48 } 49 #pragma mark - NSURLConnectionDataDelegate的代理方法 50 //請求失敗時調(diào)用 51 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 52 { 53 NSLog(@"didFailWithError"); 54 } 55 //接收到服務(wù)器響應(yīng)就會調(diào)用 56 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 57 { 58 //NSLog(@"didReceiveResponse"); 59 self.fileData = [NSMutableData data]; 60 61 //取出文件的總長度 62 //NSHTTPURLResponse *resp = (NSHTTPURLResponse *)response; 63 //long long fileLength = [resp.allHeaderFields[@"Content-Length"] longLongValue]; 64 65 self.totalLength = response.expectedContentLength; 66 } 67 //當(dāng)接收到服務(wù)器返回的實(shí)體數(shù)據(jù)時就會調(diào)用(這個方法根據(jù)數(shù)據(jù)的實(shí)際大小可能被執(zhí)行多次) 68 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 69 { 70 //拼接數(shù)據(jù) 71 [self.fileData appendData:data]; 72 73 //設(shè)置進(jìn)度條(0~1) 74 self.progressView.progress = (double)self.fileData.length / self.totalLength; 75 76 NSLog(@"didReceiveData -> %ld",self.fileData.length); 77 } 78 //加載完畢時調(diào)用(服務(wù)器的數(shù)據(jù)完全返回后) 79 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 80 { 81 //NSLog(@"connectionDidFinishLoading"); 82 //拼接文件路徑 83 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 84 NSString *file = [cache stringByAppendingPathComponent:@"minion_01.mp4"]; 85 //NSLog(@"%@",file); 86 87 //寫到沙盒之中 88 [self.fileData writeToFile:file atomically:YES]; 89 } 90 91 @end?二、內(nèi)存優(yōu)化
1 // 2 // ViewController.m 3 // IOS_0201_大文件下載(合理方式) 4 // 5 // Created by ma c on 16/2/1. 6 // Copyright ? 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController ()<NSURLConnectionDataDelegate> 12 13 ///用來寫數(shù)據(jù)的句柄對象 14 @property (nonatomic, strong) NSFileHandle *writeHandle; 15 ///文件總大小 16 @property (nonatomic, assign) long long totalLength; 17 ///當(dāng)前已寫入文件大小 18 @property (nonatomic, assign) long long currentLength; 19 20 @end 21 22 @implementation ViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 27 self.view.backgroundColor = [UIColor cyanColor]; 28 29 } 30 31 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 32 { 33 [self download]; 34 } 35 36 - (void)download 37 { 38 //1.NSURL 39 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/minion_02.mp4"]; 40 //2.請求 41 NSURLRequest *request = [NSURLRequest requestWithURL:url]; 42 //3.下載(創(chuàng)建完conn后會自動發(fā)起一個異步請求) 43 [NSURLConnection connectionWithRequest:request delegate:self]; 44 45 } 46 #pragma mark - NSURLConnectionDataDelegate的代理方法 47 //請求失敗時調(diào)用 48 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 49 { 50 51 } 52 //接收到服務(wù)器響應(yīng)時調(diào)用 53 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 54 { 55 //文件路徑 56 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 57 NSString *file = [cache stringByAppendingPathComponent:@"minion_02.mp4"]; 58 NSLog(@"%@",file); 59 //創(chuàng)建一個空文件到沙盒中 60 NSFileManager *fileManager = [NSFileManager defaultManager]; 61 [fileManager createFileAtPath:file contents:nil attributes:nil]; 62 63 //創(chuàng)建一個用來寫數(shù)據(jù)的文件句柄 64 self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:file]; 65 66 //文件總大小 67 self.totalLength = response.expectedContentLength; 68 69 } 70 //接收到服務(wù)器數(shù)據(jù)時調(diào)用(根據(jù)文件大小,調(diào)用多次) 71 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 72 { 73 //移動到文件結(jié)尾 74 [self.writeHandle seekToEndOfFile]; 75 //寫入數(shù)據(jù) 76 [self.writeHandle writeData:data]; 77 //累計文件長度 78 self.currentLength += data.length; 79 NSLog(@"下載進(jìn)度-->%lf",(double)self.currentLength / self.totalLength); 80 81 } 82 //從服務(wù)器接收數(shù)據(jù)完畢時調(diào)用 83 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 84 { 85 86 self.currentLength = 0; 87 self.totalLength = 0; 88 //關(guān)閉文件 89 [self.writeHandle closeFile]; 90 self.writeHandle = nil; 91 92 } 93 @end?三、斷點(diǎn)續(xù)傳
1 // 2 // ViewController.m 3 // IOS_0201_大文件下載(合理方式) 4 // 5 // Created by ma c on 16/2/1. 6 // Copyright ? 2016年 博文科技. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController ()<NSURLConnectionDataDelegate> 12 13 ///用來寫數(shù)據(jù)的句柄對象 14 @property (nonatomic, strong) NSFileHandle *writeHandle; 15 ///連接對象 16 @property (nonatomic, strong) NSURLConnection *conn; 17 18 ///文件總大小 19 @property (nonatomic, assign) long long totalLength; 20 ///當(dāng)前已寫入文件大小 21 @property (nonatomic, assign) long long currentLength; 22 23 - (IBAction)btnClick:(UIButton *)sender; 24 @property (weak, nonatomic) IBOutlet UIButton *btnClick; 25 26 @property (weak, nonatomic) IBOutlet UIProgressView *progressView; 27 28 @end 29 30 @implementation ViewController 31 32 - (void)viewDidLoad { 33 [super viewDidLoad]; 34 35 self.view.backgroundColor = [UIColor cyanColor]; 36 //設(shè)置進(jìn)度條起始狀態(tài) 37 self.progressView.progress = 0.0; 38 39 } 40 41 #pragma mark - NSURLConnectionDataDelegate的代理方法 42 //請求失敗時調(diào)用 43 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 44 { 45 46 } 47 //接收到服務(wù)器響應(yīng)時調(diào)用 48 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 49 { 50 if (self.currentLength) return; 51 //文件路徑 52 NSString *cache = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; 53 NSString *file = [cache stringByAppendingPathComponent:@"minion_02.mp4"]; 54 NSLog(@"%@",file); 55 //創(chuàng)建一個空文件到沙盒中 56 NSFileManager *fileManager = [NSFileManager defaultManager]; 57 [fileManager createFileAtPath:file contents:nil attributes:nil]; 58 59 //創(chuàng)建一個用來寫數(shù)據(jù)的文件句柄 60 self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:file]; 61 62 //文件總大小 63 self.totalLength = response.expectedContentLength; 64 65 } 66 //接收到服務(wù)器數(shù)據(jù)時調(diào)用(根據(jù)文件大小,調(diào)用多次) 67 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 68 { 69 //移動到文件結(jié)尾 70 [self.writeHandle seekToEndOfFile]; 71 //寫入數(shù)據(jù) 72 [self.writeHandle writeData:data]; 73 //累計文件長度 74 self.currentLength += data.length; 75 //設(shè)置進(jìn)度條進(jìn)度 76 self.progressView.progress = (double)self.currentLength / self.totalLength; 77 78 NSLog(@"下載進(jìn)度-->%lf",(double)self.currentLength / self.totalLength); 79 80 81 } 82 //從服務(wù)器接收數(shù)據(jù)完畢時調(diào)用 83 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 84 { 85 86 self.currentLength = 0; 87 self.totalLength = 0; 88 //關(guān)閉文件 89 [self.writeHandle closeFile]; 90 self.writeHandle = nil; 91 //下載完成后,狀態(tài)取反 92 self.btnClick.selected = !self.btnClick.isSelected; 93 } 94 95 - (IBAction)btnClick:(UIButton *)sender { 96 //狀態(tài)取反 97 sender.selected = !sender.isSelected; 98 if (sender.selected) { //繼續(xù)下載 99 //1.NSURL 100 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos/minion_02.mp4"]; 101 //2.請求 102 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 103 104 //設(shè)置請求頭 105 NSString *range = [NSString stringWithFormat:@"bytes=%lld-",self.currentLength]; 106 [request setValue:range forHTTPHeaderField:@"Range"]; 107 108 //3.下載(創(chuàng)建完conn后會自動發(fā)起一個異步請求) 109 self.conn = [NSURLConnection connectionWithRequest:request delegate:self]; 110 111 } else { //暫停下載 112 [self.conn cancel]; 113 self.conn = nil; 114 115 } 116 117 } 118 @end?
轉(zhuǎn)載于:https://www.cnblogs.com/oc-bowen/p/5175207.html
總結(jié)
以上是生活随笔為你收集整理的IOS-网络(大文件下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: async中series的实现 java
- 下一篇: Sublime Text添加插入带当前时