// // ViewController.m // 03-GCD-线程间通信 // // Created by mac on 16/4/21. // Copyright © 2016年 mac. All rights reserved. // #define GlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define MainQueue dispatch_get_main_queue() #import "ViewController.h" @interface ViewController () /** * oc语法中:1. 属性名不能以new开头 2. init开头的构造方法中才允许self进行赋值 */ //@property (weak, nonatomic) IBOutlet UIImageView *newImageView; @property (weak, nonatomic) IBOutlet UIButton *buttonImage; @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } /** * 属性不能以new开头,new有特殊含义 */ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { dispatch_async(GlobalQueue, ^{ //1. 子线程中下载图片 NSURL *url = [NSURL URLWithString:@"http://img0.imgtn.bdimg.com/it/u=4168762024,1922499492&fm=21&gp=0.jpg"]; NSData *data = [NSData dataWithContentsOfURL:url]; UIImage *image = [UIImage imageWithData:data]; NSLog(@"===%@,%@", image, [NSThread currentThread]); //2. 主线程中设置图片 dispatch_async(MainQueue, ^{ [self.buttonImage setBackgroundImage:image forState:UIControlStateNormal]; NSLog(@"=%@,%@", image, [NSThread currentThread]); }); }); } @end
时间: 2024-11-09 13:53:10