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

 

 

 

转载于:https://www.cnblogs.com/5ishare/p/4460668.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值