//
// ViewController.m
// IOS_0131_小文件下载
//
// Created by ma c on 16/1/31.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h" @interface ViewController () @end @implementation ViewController
/*
下载小文件的方式
1.NSData dataWithContentOfURL
2.NSURLConnection
*/ - (void)viewDidLoad {
[super viewDidLoad]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self downloadFile1];
[self downloadFile2];
} ///1.NSData dataWithContentOfURL
- (void)downloadFile1
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"downloadFile1---%ld",data.length);
});
}
///2.NSURLConnection
- (void)downloadFile2
{
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"downloadFile2---%ld",data.length); }];
} @end
04-25 05:52