下载小文件的三种方式

Posted on 2016-07-19 22:29  柠檬片  阅读(286)  评论(0)    收藏  举报

1.图片下载

1 -(void)dataDonload
2 {
3     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_02.png"];
4     NSData *data = [NSData dataWithContentsOfURL:url];
5     self.imagView.image = [UIImage imageWithData:data];
6 }

 

2.通过NSURLConnection下载

-(void)connect
{
    
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_02.png"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        self.imagView.image = [UIImage imageWithData:data];
    }];
}

 

3.通过NSURLConnection代理方式下载(可计算下载进度)

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()<NSURLConnectionDataDelegate>
 4 @property (weak, nonatomic) IBOutlet UIImageView *imagView;
 5 @property (nonatomic, strong) NSMutableData *fileData;
 6 //当前已经下载文件的大小
 7 @property (nonatomic, assign) NSInteger currentLength;
 8 //下载文件的总大小
 9 @property (nonatomic, assign) NSInteger totalLength;
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad {
15     [super viewDidLoad];
16     // Do any additional setup after loading the view, typically from a nib.
17 }
18 
19 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
20 {
21     //1.确定请求路径
22     NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_02.mp4"];
23     //2.创建请求对象
24     NSURLRequest *request = [NSURLRequest requestWithURL:url];
25     
26     //3.设置代理,发送请求
27     [NSURLConnection connectionWithRequest:request delegate:self];
28 }
29 
30 #pragma mark  NSURLConnectionDataDelegate  start
31 
32 /*
33  1.接收到服务器响应的适合调用
34  */
35 -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
36 {
37     self.fileData = [NSMutableData data];
38     
39     //拿到文件的总大小
40     self.totalLength = response.expectedContentLength;
41     
42     NSLog(@"%zd",self.totalLength);
43 }
44 
45 /*
46  2.接收到服务器返回的数据,会调用多次
47  */
48 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
49 {
50     [self.fileData appendData:data];
51    
52     
53     self.currentLength = self.fileData.length;
54 //     NSLog(@"%zd---%zd",data.length,self.currentLength);
55     
56     NSLog(@"%f",1.0 * self.currentLength / self.totalLength);
57 }
58 
59 /*
60  3.当请求完成之后调用该方法
61  */
62 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
63 {
64     NSLog(@"connectionDidFinishLoading");
65     
66     //保存下载的文件到沙河
67     NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
68     
69     //拼接文件全路径
70     NSString *fullPath = [caches stringByAppendingPathComponent:@"abc.mp4"];
71     
72     //写入数据到文件
73     [self.fileData writeToFile:fullPath atomically:YES];
74     NSLog(@"%@",fullPath);
75 //    self.imagView.image = [UIImage imageWithData:self.fileData];
76 }
77 /*
78  4.当请求失败的适合调用该方法,如果失败那么error有值
79  */
80 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
81 {
82     NSLog(@"didFailWithError");
83 }
84 
85 #pragma mark  NSURLConnectionDataDelegate  end
86 @end
通过代理下载