网络之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

网络之NSURLConnection

 

你可能感兴趣的:(urlconnection)