通常来说,网络的的获取方法,是使用NSURL,NSURLRequest,NSURLConnection,这三个类,但是其实都是使用ASIHttp的第三方类,去获得网络信息。
具体的参考实例如下:
// 判断网络是否通
if ([[ReachabilityreachabilityForInternetConnection] currentReachabilityStatus]!=kNotReachable) {
NSURL *url = [NSURLURLWithString:imagePath];
// 显示进度 MBProgressHUD *loadingView = [[[MBProgressHUDalloc]initWithView:self.view]autorelease]; loadingView.labelText = @"正在加载..."; [self.view addSubview:loadingView]; [loadingView setMode:MBProgressHUDModeIndeterminate]; loadingView.taskInProgress = YES; [loadingView show:YES];
// 直接获取,关键点是通过一个block实现,这个方法就不需要通过delegate去实现了。 ASIHTTPRequest *httpRequest = [ASIHTTPRequestrequestWithURL:url];
[httpRequest setCompletionBlock:^{
[loadingView hide:YES];
UIImage *downloadedImage = [UIImageimageWithData:[httpRequest responseData]];
progressViewBlue.hidden =YES;
imageView.image = downloadedImage; }];
static longlong downloadedBytes = 0;
[httpRequest setBytesReceivedBlock:^(unsignedlong long size, unsigned long long total){ NSLog(@"size:%lld,total:%lld",size,total); downloadedBytes += size; CGFloat progressPercent = (CGFloat)downloadedBytes/total;
loadingView.progress = progressPercent;
progressViewBlue.progress = progressPercent; progressViewYellow.progress = progressPercent; progressLabel.text = [NSStringstringWithFormat  "%.0f%%",progressPercent*100];
}];
[httpRequest startAsynchronous];
} else { NSLog(@"network not available"); }
这个项目涉及的第三方类: 1、Reachability.h,去判断网络是否通。 2、ASIHTTPRequest.h 请求。 3、MBProgressHUD.h 进度显示。
|