数据库总结完之后,下面来总结下网络这块,写博客的目的是为了让想学习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的更多相关文章

  1. iOS开发网络篇—NSURLConnection基本使用

    iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...

  2. iOS开发网络篇--NSURLConnection

    S简介 NSURLConnection: 作用: 1.负责发送请求,建立客户端和服务器的连接发送数据给服务器 2.并收集来自服务器的响应数据 步骤: 1.创建一个NSURL对象,设置请求路径 2.传入 ...

  3. 多线程与网络之NSURLConnection发送请求

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. 教你如何封装异步网络连接NSURLConnection实现带有百分比的下载

    教你如何封装异步网络连接NSURLConnection实现带有百分比的下载 注:本教程需要你对block有着较为深刻的理解,且对如何封装对象有着一些经验. 也许你已经用惯了AFNetworking2. ...

  5. swift开发网络篇—NSURLConnection基本使用

    iOS开发网络篇—NSURLConnection基本使用 一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据 ...

  6. iOS网络1——NSURLConnection使用详解

    原文在此 一.整体介绍 NSURLConnection是苹果提供的原生网络访问类,但是苹果很快会将其废弃,且由NSURLSession(iOS7以后)来替代.目前使用最广泛的第三方网络框架AFNetw ...

  7. 网络&热恋NSURLConnection代理及GET¥POST请求

    1.NSURLConnection代理下载设置在本地的身骑着白马MP3 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  8. iOS开发网络篇—NSURLConnection基本使用(一)

      一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.. ...

  9. Ios之网络编程NSURLConnection

    通过NSURLConnection主要通过四个类进行网络访问:NSURL,NSURLRequest,NSMutableURLRequest,NSURLConnection 一.基本知识 (1)NSUR ...

随机推荐

  1. [Phalcon-framework]2016-04-13_安装使用 Phalcon 框架

    1. 获取你的 PHP Version,操作系统是 x86 还是 64bit的,以及 Compiler 是什么 VC, 你可以直接同时 phpinfo() 函数获取到,如下截图: 2.  下载对应的 ...

  2. vsm安装

    一. 部署环境介绍 软件需求 1) CentOS 6.5 64bit 2) Ceph 0.80.6 网络拓扑介绍 1) Controller Node 由Console节点组成,安装VSM控制平台 2 ...

  3. 微擎开启redis memcache文档2

    微擎开启redis memcache 2018年01月20日 14:39:54 luogan129 阅读数:2161更多 个人分类: 微信开发   版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  4. Android-Kotlin-Activity直接的跳转

    1.选中应用包名packageName,右键: 2.选中Kotlin: 3.创建Kotlin的Activity完成: 第一个Activity,MainActivity package cn.kotli ...

  5. string转Date转回String(JAVA)

    String dateString = "2012-12-06 ";   SimpleDateFormat sdf = new SimpleDateFormat("yyy ...

  6. [ASE][Daily Scrum]11.06

    我们的<坦克大战·无尽>正式开始动工了,今天的任务计划如下~ [Plan] View Shilin Liu 搭建好开发环境 收集素材         Control Jiafan Zhu ...

  7. Excel 多个单元格输入同样内容

    step1: 将这些单元格选定.方法:可以连续选,也可以 ctrl + select不连续选择: step2:输入你想输入的内容,PS:出现在最后选择的单元格中: step3:组合键:ctrl + e ...

  8. Facebook内部报告:争取青少年用户的鸡贼小技巧

    翻译:吴祺深 欢迎访问网易云社区,了解更多网易技术产品运营经验. 去年十月,Facebook收购了TBH,最后却关闭了这款APP,不过一则内部报告透露了,通过这款流行的投票APP,这家公司学会了如何去 ...

  9. 转---写一个网页进度loading

    作者:jack_lo www.jianshu.com/p/4c93f5bd9861 如有好文章投稿,请点击 → 这里了解详情 loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在 ...

  10. 爬虫 解码gb1312类型

    今天我爬虫的时候竟然遇见了网页编码为gb1312类型的网站  , 不是平常的utf-8 遇到这种类型的时候忽视它是最好的办法 ① respond.content.decode('gb18030','i ...