简单介绍两种小文件下载的方法
- (void)downloadFile { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 其实这就是一个GET请求 NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/images/minion_01.png"]; NSData *data = [NSData dataWithContentsOfURL:url]; NSLog(@"%d", 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 *response, NSData *data, NSError *connectionError) { NSLog(@"%d", data.length); }]; }
时间: 2024-10-21 04:04:54