iOS GCD实现线程间通信

#import"ViewController.h"

@interfaceViewController()

@property(weak,nonatomic)IBOutletUIImageView*imageView;

@end

@implementationViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event

{

//1.创建子线程下载图片

//DISPATCH_QUEUE_PRIORITY_DEFAULT 0

dispatch_async(dispatch_get_global_queue(0,0), ^{

//1.1确定url

NSURL*url = [NSURLURLWithString:@"http://a.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=da0ec79c738da9774e7a8e2f8561d42f/c83d70cf3bc79f3d6842e09fbaa1cd11738b29f9.jpg"];

//1.2下载二进制数据到本地

NSData*imageData =[NSDatadataWithContentsOfURL:url];

//1.3转换图片

UIImage*image = [UIImageimageWithData:imageData];

NSLog(@"download----%@",[NSThreadcurrentThread]);

//更新UI

//dispatch_async(dispatch_get_main_queue(), ^{

dispatch_sync(dispatch_get_main_queue(), ^{

self.imageView.image= image;

NSLog(@"UI----%@",[NSThreadcurrentThread]);

});

});

}

你可能感兴趣的:(iOS GCD实现线程间通信)