zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

网络之NSURLConnection

2023-09-14 08:57:58 时间
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

 



iOS开发网络篇—NSURLConnection基本使用 iOS开发网络篇—NSURLConnection基本使用 一、NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法、请求头、请求体.... (3)NSMutableURLRequest:NSURLRequest的子类 (4)NSURLConnection:负责发送请求,建立客户端和服务器的连接。
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.