数据库总结完之后,下面来总结下网络这块,写博客的目的是为了让想学习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. <c:forEach varStatus="status">中 varStatus的属性简介

    我们常会用c标签来遍历需要的数据,为了方便使用,varStatus属性可以方便我们实现一些与行数相关的功能,如:奇数行.偶数行差异:最后一行特殊处理等等.先就varStatus属性常用参数总结下: $ ...

  2. Python自动化开发 -进程、线程和协程(二)

    本节内容 一.线程进程介绍 二. 线程 1.线程基本使用 (Threading) 2.线程锁(Lock.RLock) 3.信号量(Semaphore) 4.事件(event) 5.条件(Conditi ...

  3. 安装json插件

    谷歌浏览器中安装JsonView扩展程序 实际开发工作中经常用到json数据,那么就会有这样一个需求:在谷歌浏览器中访问URL地址返回的json数据能否按照json格式展现出来. 比如,在谷歌浏览器中 ...

  4. jacoco初探

    # 背景 集团的代码覆盖率平台因为网络问题无法使用,只能自己研究下. 覆盖率是衡量自动化用例效果产品的一个指标,但只是一个辅助指标,覆盖率高并不意味着质量好,但覆盖率低却能说明一些问题, # 对比 覆 ...

  5. 自定义WPF窗体形状

    介绍 你好WPF爱好者. 随着WPF等统一API语言的发明,丰富用户界面变得非常容易. 创建丰富的用户界面只是一个想法. 您需要拥有的是创造性思维和最新技术融合. WPF和Expression Ble ...

  6. python中硬要写抽象类和抽象方法

    由于python没有抽象类.接口的概念,所以要实现这种功能得abc.py这个类库,具体方式如下: # coding: utf-8import abc #抽象类class StudentBase(obj ...

  7. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

  8. 容器监控:cadvisor+influxdb+grafana

    cAdvisor:Google开源的工具,用于监控Docker主机和容器系统资源,通过图形页面实时显示数据,但不存储:它通过宿主机/proc./sys./var/lib/docker等目录下文件获取宿 ...

  9. 面试题-lazyMan实现

    原文:如何实现一个LazyMan 面试题目 实现一个LazyMan,可以按照以下方式调用: LazyMan('Hank'),输出: Hi, This is Hank! LazyMan('Hank'). ...

  10. D17——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D17 20181014内容纲要: 1.jQuery介绍 2.jQuery功能介绍 (1)jQuery的引入方式 (2)选择器 (3)筛选 (4)文本操作 (5) ...