博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络之NSURLConnection
阅读量:5887 次
发布时间:2019-06-19

本文共 4080 字,大约阅读时间需要 13 分钟。

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

 

 

 

转载地址:http://gorix.baihongyu.com/

你可能感兴趣的文章
WeChat Official Account Admin Platform API Introduction
查看>>
C语言写单链表的创建、释放、追加(即总是在最后的位置增加节点)
查看>>
poj1635
查看>>
C# LINQ详解(一)
查看>>
视频直播点播nginx-rtmp开发手册中文版
查看>>
ruby学习总结04
查看>>
Binary Tree Paths
查看>>
Ueditor自定义ftp上传
查看>>
线程以及多线程
查看>>
PHP队列的实现
查看>>
单点登录加验证码例子
查看>>
[T-SQL]从变量与数据类型说起
查看>>
稀疏自动编码之反向传播算法(BP)
查看>>
二叉搜索树转换成双向链表
查看>>
WebLogic和Tomcat的区别
查看>>
java类中 获取服务器的IP 端口
查看>>
调用约定__stdcall / __cdecl
查看>>
occActiveX - ActiveX with OpenCASCADE
查看>>
redmine
查看>>
css 序
查看>>