这是苹果SDK自带的一个下载类,使用简单;
首先要接受<NSURLConnectionDataDelegate>协议
- (void)viewDidLoad
{
[super viewDidLoad];
_data = [[NSMutableData alloc]init];
_imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[[self view]addSubview:_imageView];
NSString * path = @"http://d.hiphotos.baidu.com/image/w%3D2048/sign=17bd9a6396eef01f4d141fc5d4c69825/94cad1c8a786c917dc0c713ecb3d70cf3bc757ad.jpg";
[self startDownloadWithPath:path];
}
//实现现在的协议方法
-(void)startDownloadWithPath:(NSString*)path{
NSURL * url = [[NSURL alloc]initWithString:path];
NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30];
_connection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[_data setLength:0];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
_imageView.image = [UIImage imageWithData:_data];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
}