通常来说,网络的的获取方法,是使用NSURL,NSURLRequest,NSURLConnection,这三个类,但是其实都是使用ASIHttp的第三方类,去获得网络信息。
具体的参考实例如下:
// 判断网络是否通
if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus]!=kNotReachable)
{
NSString *imagePath = @"http://www.weather.com.cn/m/i/weatherpic/29x20/d0.gif";// @"http://img.evolife.cn/2011-07/ad1e1823f6c67c75.jpg";
NSURL *url = [NSURL URLWithString:imagePath];
// 显示进度
MBProgressHUD *loadingView = [[[MBProgressHUD alloc]initWithView:self.view]autorelease];
loadingView.labelText = @"正在加载...";
[self.view addSubview:loadingView];
[loadingView setMode:MBProgressHUDModeIndeterminate];
loadingView.taskInProgress = YES;
[loadingView show:YES];
// 直接获取,关键点是通过一个block实现,这个方法就不需要通过delegate去实现了。
ASIHTTPRequest *httpRequest = [ASIHTTPRequest requestWithURL:url];
[httpRequest setCompletionBlock:^{
[loadingView hide:YES];
UIImage *downloadedImage = [UIImage imageWithData:[httpRequest responseData]];
progressViewBlue.hidden = YES;
imageView.image = downloadedImage;
}];
static long long downloadedBytes = 0;
[httpRequest setBytesReceivedBlock:^(unsigned long 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 = [NSString stringWithFormat:@"%.0f%%",progressPercent*100];
}];
[httpRequest startAsynchronous];
}
else
{
NSLog(@"network not available");
}
这个项目涉及的第三方类:
1、Reachability.h,去判断网络是否通。
2、ASIHTTPRequest.h 请求。
3、MBProgressHUD.h 进度显示。
4、进度条,彩色的WNProgressView.h。