@interface ViewController ()
{
UIImageView *_imgView;
NSMutableData *_mData;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
_imgView.backgroundColor = [UIColor redColor];
[self.view addSubview:_imgView];
//初始化Data
// _mData = [NSMutableData data];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//请求网络,加载图片
/*
http://a.hiphotos.baidu.com/image/pic/item/342ac65c103853439b0d4d9a9013b07ecb80884f.jpg
*/
- (IBAction)btnClick:(UIButton *)sender {
for (int i = 0; i < 3; i++) {
//创建imgView
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100+ i*100, 100, 100)];
imgView.backgroundColor = [UIColor purpleColor];
[self.view addSubview:imgView];
//1、构建URL
NSURL *url = [NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/pic/item/342ac65c103853439b0d4d9a9013b07ecb80884f.jpg"];
//2、构建请求对象
//NSURLRequest为不可变对象
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
//设置请求方式
[request setHTTPMethod:@"GET"];
//设置请求超时的时间
[request setTimeoutInterval:60];
//3、构建连接对象,并且发送
//同步请求
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSLog(@"data is %@", data);
NSLog(@"response is %@", response);
// [_imgView setImage:[UIImage imageWithData:data]];
[imgView setImage:[UIImage imageWithData:data]];
}
}
//异步请求网络
- (IBAction)buttonASynAction:(id)sender {
//清空数据
_mData = [NSMutableData data];
//1、构建URL
NSURL *url = [NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/pic/item/342ac65c103853439b0d4d9a9013b07ecb80884f.jpg"];
//2、构建请求对象
//NSURLRequest为不可变对象
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
//设置请求方式
[request setHTTPMethod:@"GET"];
//设置请求超时的时间
[request setTimeoutInterval:60];
//3、构建连接对象,发送异步请求
//第一种方式
//创建一个线程队列
// NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// [NSURLConnection sendAsynchronousRequest:request
// queue:queue
// completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// /*
// response:包含了服务器的响应信息
// data: 加载完之后的数据
// connectionError: 如果请求失败了,包含了失败的信息
// */
// if (connectionError) {
// NSLog(@"网络请求失败,error is %@", connectionError);
// return;
// }
//
// //回到主线程进行UI刷新的操作
//// [self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>]
// dispatch_async(dispatch_get_main_queue(), ^{
// _imgView.image = [UIImage imageWithData:data];
// });
//
// }];
// //第二种方式
// NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
// //取消网络请求
//// [connection cancel];
//第三种方法
// [self performSelectorInBackground:<#(SEL)#> withObject:<#(id)#>]
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
_imgView.image = [UIImage imageWithData:data];
});
});
}
#pragma mark -NSURLConnectionDelegate(父协议)
#pragma mark -NSURLConnectionDataDelegate(子协议)
#pragma mark -NSURLConnectionDownloadDelegate(子协议)
//已经与服务器进行了连接
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"didReceiveResponse: response is %@", response);
}
//已经开始接收数据了,这个方法会调用多次
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"已经接收到数据了");
[_mData appendData:data];
}
//已经完成了连接,此方法调用时,所有数据都已经接收完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"连接完成");
_imgView.image = [UIImage imageWithData:_mData];
}
@end
866

被折叠的 条评论
为什么被折叠?



