在.h文件中写入 需要使用 NSURLConnectionDataDelegate 协议
@property (nonatomic, assign) IBOutlet UIImageView *imageView;
- (IBAction)download;
在.m文件中写入
@property (nonatomic, retain) NSMutableData *data; 定义一个数组
//释放资源
- (void)dealloc {
[_data release];
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
self.data = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)download {
self.data = [NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://192.168.1.106:8080/meitu.png"];
NSURLRequest *reuqest = [NSURLRequest requestWithURL:url];
[[NSURLConnection connectionWithRequest:reuqest delegate:self] start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.data appendData:data];
}
//下载结束后调用的方法
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
UIImage *image = [UIImage imageWithData:self.data];
NSLog(@"下载完毕");
self.imageView.image = image;
}