网络之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 ...
随机推荐
- 走上模拟道路 HDU4891
http://vjudge.net/contest/view.action?cid=51327#problem/D Description Yoda: May the Force be with yo ...
- PL/SQL客户端连Oracle很快就断开问题的解决
PL/SQL登录很短时间session就自动断开 1.首先查看你这个用户的profile文件 select profile from dba_users where username='USERNAM ...
- iTerm2连接远程-中文乱码问题
现象 mac 上用是iterm2终端, Shell 环境是zsh. ssh 到Linux 服务器上查看一些文件时,中文乱码. 这种情况一般是终端和服务器的字符集不匹配,MacOSX下默认的是utf8 ...
- 前端基础-html 字体标签,排版标签,超链接,图片标签
主要内容: 字体标签: h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...
- day 74 vue 2 axios数据请求 以及组件的学习
前情提要: vue 学习二: 一: 通过axios实现数据请求 1:json数据语法 json数据对象类似于JavaScript中的对象,但是它的键对应的值里面是没有函数方法的,值可以是普通变量, ...
- 将python打包为.exe文件
第一步:在https://pypi.python.org/pypi/PyInstaller/2.1 下载pyinstaller. 第二步:解压缩,在该目录下命令行中执行python setup.py ...
- [原创]K8 MSF Bind Shell TCP 连接工具
工具: K8_MSFBindShellClient_20170524[K.8]编译: 自己查壳组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.blog.1 ...
- python多线程--Condition(条件对象)
Condition class threading.Condition(lock=None 这个类实现条件变量对象.条件变量允许一个或多个线程等待,知道它们被另一个线程唤醒. 如果给出了lock参数而 ...
- (转)Python3入门之线程threading常用方法
原文:https://www.cnblogs.com/chengd/articles/7770898.html https://blog.csdn.net/sunhuaqiang1/article/d ...
- PHP使用APC获取上传文件进度
今天发现使用PHP的APC也能获取上传文件的进度.这篇文章就说下如何做. 安装APC 首先安装APC的方法和其他PHP模块的方法没什么两样,网上能找出好多 phpinfo可以看到APC的默认配置有: ...