网络之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 ...
随机推荐
- HDU 3078 LCA转RMQ
题意: n个点 m个询问 下面n个数字表示点权值 n-1行给定一棵树 m个询问 k u v k为0时把u点权值改为v 或者问 u-v的路径上 第k大的数 思路: LCA转RMQ求出 LCA(u,v) ...
- scikit-FEM-mesh
# -*- coding: utf-8 -*- """ “Mesh”模块包含了有限元网格的不同类型. See the following implementations: ...
- Python自动化开发 - 函数式编程
本节内容 一.函数式编程 二.高阶函数 1.变量可以指向函数 2.函数名也是变量 3.传入函数 三.返回函数 1.函数作为返回值 2.闭包特性 一.函数式编程 函数是Python内建支持的一种封装,我 ...
- cxGrid之checkbox小结
http://www.cnblogs.com/Kim53622744/p/4428997.html 在cxgrid中增加选择列 1.在dataset(query/table/clientdataset ...
- ELK冷热数据分离
通常情况下,我们使用ELK日志分析平台最常用的数据时间为1周或一个月(因业务场景不同,可能存在差别),时间比较长的数据没有特殊情况可能我们就没有必要再进行查询了,但是因业务需求或者作为凭证,这些日 ...
- Gimp RGB 转 CMYK
安装GIMP separate+插件.在Windows的Photoshop中,有转换CMYK的功能,非常简单.在Linux里,通常用GIMP进行转换.由于授权的问题,默认安装的GIMP里没有安装转换的 ...
- Kali Linux渗透测试实战 1.4 小试牛刀
目录 1.4 小试牛刀 1.4.1 信息搜集 whois查询 服务指纹识别 端口扫描 综合性扫描 1.4.2 发现漏洞 1.4.3 攻击与权限维持 小结 1.4 小试牛刀 本节作为第一章的最后一节,给 ...
- 统计--VARCHAR与NVARCHAR在统计预估上的区别
最近遇到一个问题,当查询使用到模糊查询时,由于预估返回行数过高,执行计划认为索引查找+Key Lookup的成本过高,因此采用Clustered Index Scan的方式,消耗大量逻辑IO,执行计划 ...
- 使用C#写MVC框架(一:核心原理)
目录: 一.MVC原理解析 二.HttpHandler 1.HttpHandler,IHttpHandler,MvcHandler的说明 2.IHttpHandler解析 3.MvcHandler解析 ...
- win10安装Ubuntu双系统
1.软碟通做启动盘,不要用easyBCD,比较麻烦 2.windows10中取消选择"启用快速启动(推荐)" 3.压缩出空白卷 4.重启时按F12 5.在bios中将boot pr ...