网络之NSURLConnection
数据库总结完之后,下面来总结下网络这块,写博客的目的是为了让想学习IOS的不用去培训机构就能学习。
//
// ViewController.m
// UrlConnection
//
// Created by City--Online on 15/4/27.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#define imageUrl @"http://assets.sbnation.com/assets/2512203/dogflops.gif"
#import "ViewController.h"
@interface ViewController ()<NSURLConnectionDataDelegate>
{
UIImageView *imageView;
UIActivityIndicatorView *indicatorView;
UIProgressView *progessView;
UILabel *progressLabel;
NSMutableData *imgData;
long long allBytes;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
imgData=[NSMutableData data];
[self initUI];
[self startDownLoadImage];
}
-(void)startDownLoadImage
{
NSURL *url=[NSURL URLWithString:imageUrl];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
// block
// NSOperationQueue *queue=[NSOperationQueue mainQueue];
// [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// if (data) {
// imageView.image=[UIImage imageWithData:data];
// [indicatorView stopAnimating];
//
// }
// }];
// 代理
[NSURLConnection connectionWithRequest:request delegate:self];
// NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
// [connection start];
}
//连接失败
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"连接失败");
}
//获得响应
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
imgData.length=0;
//获取文件大小
allBytes=[response expectedContentLength];
//获取文件名
NSString *filename=[response suggestedFilename];
NSLog(@"文件名:%@",filename);
//获取文件类型
NSString *contentType=[response MIMEType];
NSLog(@"文件类型:%@",contentType);
//状态码
NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse*)response;
NSInteger statusCode=[httpResponse statusCode];
NSLog(@"状态码:%ld",statusCode);
//响应头信息
NSDictionary *allHeaderFields=[httpResponse allHeaderFields];
NSLog(@"%@",allHeaderFields);
}
//接收到数据
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
//追加数据
[imgData appendData:data];
//计算进度
CGFloat progress=(CGFloat)imgData.length/allBytes;
progessView.progress=progress;
progressLabel.text=[NSString stringWithFormat:@"%2f",progress];
NSLog(@"%f",progress);
}
//响应完成
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
imageView.image=[UIImage imageWithData:imgData];
[indicatorView stopAnimating];
}
-(void)initUI
{
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 300, 300)];
//默认图片
imageView.image = [UIImage imageNamed:@"photo"];
[self.view addSubview:imageView];
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicatorView.frame = CGRectMake(0, 0, 60, 60);
indicatorView.center = CGPointMake(imageView.frame.size.width/2, imageView.frame.size.height/2);
//indicatorView.hidesWhenStopped = NO;
[indicatorView startAnimating];
[imageView addSubview:indicatorView];
//CGRectGetMaxY(imageView.frame) == imageView.frame.origin.y + imageView.frame.size.height
//CGRectGetMaxX(progessLabel.frame) == progessLabel.frame.origin.x + progessLabel.frame.size.width
//进度条
progessView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
progessView.frame = CGRectMake(imageView.frame.origin.x, CGRectGetMaxY(imageView.frame)+20, 200, 20);
[self.view addSubview:progessView];
progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(progessView.frame), progessView.frame.origin.y - 10, 80, 20)];
progressLabel.text = @"0.00";
progressLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:progressLabel];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end


网络之NSURLConnection的更多相关文章
- iOS开发网络篇—NSURLConnection基本使用
iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...
- iOS开发网络篇--NSURLConnection
S简介 NSURLConnection: 作用: 1.负责发送请求,建立客户端和服务器的连接发送数据给服务器 2.并收集来自服务器的响应数据 步骤: 1.创建一个NSURL对象,设置请求路径 2.传入 ...
- 多线程与网络之NSURLConnection发送请求
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 教你如何封装异步网络连接NSURLConnection实现带有百分比的下载
教你如何封装异步网络连接NSURLConnection实现带有百分比的下载 注:本教程需要你对block有着较为深刻的理解,且对如何封装对象有着一些经验. 也许你已经用惯了AFNetworking2. ...
- swift开发网络篇—NSURLConnection基本使用
iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...
- iOS网络1——NSURLConnection使用详解
原文在此 一.整体介绍 NSURLConnection是苹果提供的原生网络访问类,但是苹果很快会将其废弃,且由NSURLSession(iOS7以后)来替代.目前使用最广泛的第三方网络框架AFNetw ...
- 网络&热恋NSURLConnection代理及GET¥POST请求
1.NSURLConnection代理下载设置在本地的身骑着白马MP3 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...
- iOS开发网络篇—NSURLConnection基本使用(一)
一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.. ...
- Ios之网络编程NSURLConnection
通过NSURLConnection主要通过四个类进行网络访问:NSURL,NSURLRequest,NSMutableURLRequest,NSURLConnection 一.基本知识 (1)NSUR ...
随机推荐
- Python自动化开发 - 常用模块(二)
本节内容 1.shutil模块 2.shelve模块 3.xml处理模块 4.configparser模块 5.hashlib模块 6.subprocess模块 7.re模块 一.shutil模块 高 ...
- jQuery WeUI V0.4.2 发布
http://www.oschina.net/news/71590/jquery-weui-v0-4-2 jQuery WeUI V0.4.2 发布了! jQuery WeUI 中使用的是官方WeUI ...
- python网络爬虫抓取网站图片
本文介绍两种爬取方式: 1.正则表达式 2.bs4解析Html 以下为正则表达式爬虫,面向对象封装后的代码如下: import urllib.request # 用于下载图片 import os im ...
- StriveEngine-TCP
文章中的StriveEngine.dll版本为V3.9.0.0,源码下载请到 https://download.csdn.net/download/hanghangz/10966335 先上代码,建立 ...
- SoftWareHelper
SoftWareHelper 环境 Visual Studio 2017,.Net Framework 4.0 SDK GitHub源码:https://github.com/yanjinhuagoo ...
- 常用下载方式的区别-BT下载、磁力链接、电驴
出处:https://www.jianshu.com/p/72b7a64e5be1 打开 115 离线下载的窗口,看到支持这么多种链接,你都清楚他们是什么原理嘛?接下来我们一个一个说. 一.HTTP( ...
- js 标准二维数组变一维数组的方法
问题:[[0, 1], [2, 3], [4, 5]] -> [0, 1, 2, 3, 4, 5]? 方法一 利用es5的arr.reduce(callback[, initialValue]) ...
- 机器学习与Tensorflow(1)——机器学习基本概念、tensorflow实现简单线性回归
一.机器学习基本概念 1.训练集和测试集 训练集(training set/data)/训练样例(training examples): 用来进行训练,也就是产生模型或者算法的数据集 测试集(test ...
- SDK 上报信息 史上最全 持续更新
SDK 上报信息 史上最全 持续更新 接入SDK总会遇到各种需求,有些SDK巴不得把玩家信息全部上报到他们服务器! 以下是我接SDK遇到的, 欢迎大家补全. 上报事件 注册(按道理这个应该是SDK的功 ...
- 10-01 Java 类,抽象类,接口的综合小练习--运动员和教练
运动员和教练的案例分析 运动运和教练的案例 代码实现 /* 教练和运动员案例 乒乓球运动员和篮球运动员. 乒乓球教练和篮球教练. 为了出国交流,跟乒乓球相关的人员都需要学习英语. 请用所学知识: 分析 ...