4.0 文件下載
- 4.1 小文件下載
(1)第一種方式(NSData)
//使用NSDta直接加載網(wǎng)絡(luò)上的url資源(不考慮線程)
-(void)dataDownload
{
//1.確定資源路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"];
//2.根據(jù)URL加載對(duì)應(yīng)的資源
NSData *data = [NSData dataWithContentsOfURL:url];
//3.轉(zhuǎn)換并顯示數(shù)據(jù)
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
}
(2)第二種方式(NSURLConnection-sendAsync)
//使用NSURLConnection發(fā)送異步請(qǐng)求下載文件資源
-(void)connectDownload
{
//1.確定請(qǐng)求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"];
//2.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.使用NSURLConnection發(fā)送一個(gè)異步請(qǐng)求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//4.拿到并處理數(shù)據(jù)
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
}];
}
(3)第三種方式(NSURLConnection-delegate)
//使用NSURLConnection設(shè)置代理發(fā)送異步請(qǐng)求的方式下載文件
-(void)connectionDelegateDownload
{
//1.確定請(qǐng)求路徑
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
//2.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3.使用NSURLConnection設(shè)置代理并發(fā)送異步請(qǐng)求
[NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark--NSURLConnectionDataDelegate
//當(dāng)接收到服務(wù)器響應(yīng)的時(shí)候調(diào)用撇眯,該方法只會(huì)調(diào)用一次
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//創(chuàng)建一個(gè)容器面粮,用來(lái)接收服務(wù)器返回的數(shù)據(jù)
self.fileData = [NSMutableData data];
//獲得當(dāng)前要下載文件的總大小(通過(guò)響應(yīng)頭得到)
NSHTTPURLResponse *res = (NSHTTPURLResponse *)response;
self.totalLength = res.expectedContentLength;
NSLog(@"%zd",self.totalLength);
//拿到服務(wù)器端推薦的文件名稱
self.fileName = res.suggestedFilename;
}
//當(dāng)接收到服務(wù)器返回的數(shù)據(jù)時(shí)會(huì)調(diào)用
//該方法可能會(huì)被調(diào)用多次
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// NSLog(@"%s",__func__);
//拼接每次下載的數(shù)據(jù)
[self.fileData appendData:data];
//計(jì)算當(dāng)前下載進(jìn)度并刷新UI顯示
self.currentLength = self.fileData.length;
NSLog(@"%f",1.0* self.currentLength/self.totalLength);
self.progressView.progress = 1.0* self.currentLength/self.totalLength;
}
//當(dāng)網(wǎng)絡(luò)請(qǐng)求結(jié)束之后調(diào)用
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//文件下載完畢把接受到的文件數(shù)據(jù)寫入到沙盒中保存
//1.確定要保存文件的全路徑
//caches文件夾路徑
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *fullPath = [caches stringByAppendingPathComponent:self.fileName];
//2.寫數(shù)據(jù)到文件中
[self.fileData writeToFile:fullPath atomically:YES];
NSLog(@"%@",fullPath);
}
//當(dāng)請(qǐng)求失敗的時(shí)候調(diào)用該方法
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%s",__func__);
}
- Posted by *** singerYoung ***
- 聯(lián)系作者 簡(jiǎn)書·singerYoung 新浪微博·小小羊run
- 原創(chuàng)文章摔刁,版權(quán)聲明:自由轉(zhuǎn)載-非商用-非衍生-保持署名