下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
NSURLRequest 简单的网络请求- (voID)vIEwDIDLoad{ [super vIEwDIDLoad]; NSURL* url = [NSURL URLWithString:@"http://img21.mtime.cn/mg/2012/03/25/112544.76654880.jpg"]; _imageData = [[NSMutableData alloc] init]; //进度条 _pv = [[UIProgressVIEw alloc] initWithFrame:CGRectMake(50,50,220,20)]; [self.vIEw addSubvIEw:_pv]; [_pv release]; //创建一个请求 //NSURLRequest* request = [NSURLRequest requestWithURL:url]; NSURLRequest* request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30]; //发送请求 [NSURLConnection connectionWithRequest:request delegate:self];} //接收响应头- (voID)connection:(NSURLConnection *)connection dIDReceiveResponse:(NSURLResponse *)response{ NSLog(@"接收到响应头"); [_imageData setLength:0]; //拿到响应体长度 NShttpURLResponse* res = (NShttpURLResponse*)response; _length = [[res.allheaderFIElds objectForKey:@"Content-Length"] floatValue]; NSLog(@"length:%f",_length); //是否 开启 网络开关 状态栏的 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;} //接收响应体,下载数据/* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 */- (voID)connection:(NSURLConnection *)connection dIDReceiveData:(NSData *)data{ NSLog(@"接收响应体"); //把每次下载的数据添加到一起 [_imageData appendData:data]; //刷新进度条 [_pv setProgress:_imageData.length / _length animated:YES];} //成功- (voID)connectionDIDFinishLoading:(NSURLConnection *)connection{ NSLog(@"成功"); UIImage* image = [UIImage imageWithData:_imageData]; self.vIEw.backgroundcolor = [UIcolor colorWithPatternImage:image]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;} //失败- (voID)connection:(NSURLConnection *)connection dIDFailWithError:(NSError *)error{ NSLog(@"失败"); [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;}
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的NSURLRequest 简单的网络请求全部内容,希望文章能够帮你解决NSURLRequest 简单的网络请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)