线程GCD

#import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

UIImageView *imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 40, 100, 100)];

[self.view addSubview:imageview1];

UIImageView *imageview2 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 150, 100, 100)];

[self.view addSubview:imageview2];

UIImageView *imageview3 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 260, 100, 100)];

[self.view addSubview:imageview3];

UIImageView *imageview4 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 370, 100, 100)];

[self.view addSubview:imageview4];

dispatch_async(dispatch_get_global_queue(0, 0), ^{

NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.yooyoo360.com/photo/2009-1-1/20090112132752467.jpg"]];

UIImage *image = [UIImage imageWithData:data];

dispatch_async(dispatch_get_main_queue(), ^{

imageview1.image = image;

});

});

//创建组:用于存放耗时操作

dispatch_group_t group = dispatch_group_create();

__block UIImage *image1,*image2,*image3;

//将操作封装进组,第一个参数代表要存放操作的组名,第二个参数代表操作队列,block执行耗时操作

dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{

NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://pic2.ooopic.com/10/81/58/62bOOOPICce.jpg"]];

image1 = [UIImage imageWithData:data];

});

dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{

NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.downsc.com/vector_pic/shiliang_iecool/5/3/b_img/14430.jpg"]];

image2 = [UIImage imageWithData:data];

});

dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{

NSData *data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:@"http://www.downsc.com/vector_pic/shiliang_iecool/5/2/b_img/13788.jpg"]];

image3 = [UIImage imageWithData:data];

});

//监听一组操作,第一个参数代表要监听的组名,第二个参数代表一组操作完全结束后跳转到哪个队列,一般跳到主线程(dispatch_get_main_queue),block执行要进行的操作(一般用来更新UI),注意:此方法在组中的所有操作执行完毕后调用

dispatch_group_notify(group, dispatch_get_main_queue(), ^{

imageview2.image = image1;

imageview3.image = image2;

imageview4.image = image3;

});

}

线程GCD

上一篇:centos7下安装docker(18docker日志---docker logs)


下一篇:CentOS7下安装docker(Docker系列1)