标签:
线程间通信的表现:
1个线程传递数据给另1个线程
在1个线程中执行完特定任务后,转到另1个线程继续执行任务
下面是一个下载图片的例子:
@interface BTThreadViewController () { UIImageView *imagev; } @end - (void)viewDidLoad { [super viewDidLoad]; imagev = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:imagev]; //子线程里面调用downImage方法下载图片 [self performSelectorInBackground:@selector(downImage) withObject:nil]; } -(void)downImage { //从网络中下载图片 NSURL *url = [NSURL URLWithString:@"http://i8.topit.me/8/c1/31/1142319854bdc31c18o.jpg"]; //将图片转换为二进制数据 NSData *imgData = [NSData dataWithContentsOfURL:url]; //数据转换成图片 UIImage *img = [UIImage imageWithData:imgData]; //回到主线程设置图片 [self performSelectorOnMainThread:@selector(senderImage:) withObject:img waitUntilDone:NO]; } -(void)senderImage:(UIImage *)image { imagev.image = image; }
标签:
原文地址:http://my.oschina.net/linxiaoxi1993/blog/382189