iOS网络笔记--GCD

gcd的简单使用

//
//  ViewController.m
//  GCD
//
//  Created by hhg on 15/10/22.
//  Copyright (c) 2015年 hhg. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:imageView];

    //队列优先级用于数据加载
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_async(queue/*添加队列*/, ^
                   {
                       //线程要执行的方法
                       //子线程  处理数据加载
                       data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://news.xinhuanet.com/world/2015-10/22/128344315_14454681824761n.jpg"]];
                       dispatch_async(dispatch_get_main_queue(), ^
                                      {
                                          //添加一个任务到主线程,用来刷新UI
                                          imageView.image = [UIImage imageWithData:data];
                                      });
                   });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

@end

猜你喜欢

转载自blog.csdn.net/csdn_hhg/article/details/80446099