小文件下載:就以下載一個(gè)圖片視頻為例
1.//耗時(shí)操作[NSData dataWithContentsOfURL:url]
-(void)download1
{
//1.url
NSURL*url = [NSURLURLWithString:@"http://imgsrc.baidu.com/forum/w%3D580/sign=54a8cc6f728b4710ce2ffdc4f3cec3b2/d143ad4bd11373f06c0b5bd1a40f4bfbfbed0443.jpg"];
//2.下載二進(jìn)制數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfURL:url];
//3.轉(zhuǎn)換
UIImage *image = [UIImage imageWithData:data];
}
2.采用發(fā)送請(qǐng)求獲取圖片臣樱,視頻
//1.無(wú)法監(jiān)聽(tīng)進(jìn)度
//2.內(nèi)存飆升
-(void)download2
{
//1.url
// NSURL *url = [NSURL URLWithString:@"http://imgsrc.baidu.com/forum/w%3D580/sign=54a8cc6f728b4710ce2ffdc4f3cec3b2/d143ad4bd11373f06c0b5bd1a40f4bfbfbed0443.jpg"];
NSURL*url = [NSURLURLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
//2.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
//3.發(fā)送請(qǐng)求
[NSURLConnectionsendAsynchronousRequest:requestqueue:[NSOperationQueuemainQueue]completionHandler:^(NSURLResponse*_Nullableresponse,NSData*_Nullabledata,NSError*_NullableconnectionError) {
//4.轉(zhuǎn)換
//UIImage *image = [UIImage imageWithData:data];
//
//self.imageView.image = image;
//NSLog(@"%@",connectionError);
//4.寫數(shù)據(jù)到沙盒中
NSString*fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"123.mp4"];
[datawriteToFile:fullPathatomically:YES];
}];
}
3. //內(nèi)存飆升,這種方法可以監(jiān)聽(tīng)進(jìn)度
-(void)download3
{
//1.url
// NSURL *url = [NSURL URLWithString:@"http://imgsrc.baidu.com/forum/w%3D580/sign=54a8cc6f728b4710ce2ffdc4f3cec3b2/d143ad4bd11373f06c0b5bd1a40f4bfbfbed0443.jpg"];
NSURL*url = [NSURLURLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
//2.創(chuàng)建請(qǐng)求對(duì)象
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
//3.發(fā)送請(qǐng)求
[[NSURLConnectionalloc]initWithRequest:requestdelegate:self];
}
#pragma mark NSURLConnectionDataDelegate
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
NSLog(@"didReceiveResponse");
//得到文件的總大小(本次請(qǐng)求的文件數(shù)據(jù)的總大小)
self.totalSize= response.expectedContentLength;
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
// NSLog(@"%zd",data.length);
[self.fileData appendData:data];
//進(jìn)度=已經(jīng)下載/文件的總大小
NSLog(@"%f",1.0*self.fileData.length/self.totalSize);
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
NSLog(@"connectionDidFinishLoading");
//4.寫數(shù)據(jù)到沙盒中
NSString*fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES)lastObject]stringByAppendingPathComponent:@"123.mp4"];
[self.fileDatawriteToFile:fullPathatomically:YES];
NSLog(@"%@",fullPath);
}
1欺抗、同步請(qǐng)求可以從因特網(wǎng)請(qǐng)求數(shù)據(jù),一旦發(fā)送同步請(qǐng)求顷蟀,程序?qū)⑼V褂脩艚换ノ镅樱敝练?wù)器返回?cái)?shù)據(jù)完成谜疤,才可以進(jìn)行下一步操作选脊,
2澎怒、異步請(qǐng)求不會(huì)阻塞主線程褒搔,而會(huì)建立一個(gè)新的線程來(lái)操作,用戶發(fā)出異步請(qǐng)求后喷面,依然可以對(duì)UI進(jìn)行操作星瘾,程序可以繼續(xù)運(yùn)行