UI网络编程

@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


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值