标签:
子线程执行完延时操作后返回主线程,这就是通信
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 在子线程中调用download方法下载图片 [self performSelectorInBackground:@selector(download) withObject:nil]; } -(void)download { //1.根据URL下载图片 //从网络中下载图片 NSURL *urlstr=[NSURL URLWithString:@"http://cdn.duitang.com/uploads/item/201409/17/20140917231336_URiHE.jpeg"]; NSLog(@"touch down "); //把图片转换为二进制的数据 NSData *data=[NSData dataWithContentsOfURL:urlstr];//这一行操作会比较耗时 //把数据转换成图片 UIImage *image=[UIImage imageWithData:data]; //2.回到主线程中设置图片 //第一种方式 // [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO]; //第二种方式 [self.iconView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO]; } //设置显示图片 -(void)settingImage:(UIImage *)image { self.iconView.image=image; }
标签:
原文地址:http://www.cnblogs.com/sunjianfei/p/5725029.html