iOS多线程开发小demo5 线程间的通信
// DYFViewController.m // 623-06-线程间的通信 // // Created by dyf on 14-6-23. // Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved. // #import "DYFViewController.h" @interface DYFViewController () @property (weak, nonatomic) IBOutlet UIImageView *imaV; @end @implementation DYFViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.下载图片 [self performSelector:@selector(download) withObject:nil]; // 显示、刷新界面,只能在主界面 // 2.显示图片 } /** * 下载图片:子线程 */ - (void)download { // 1.根据URL下载图片 NSURL *url = [NSURL URLWithString:@"http://image.cache.xiu8.com/live/125/125/997729.jpg"]; NSLog(@"---------begin"); NSData *data = [NSData dataWithContentsOfURL:url]; // 耗时 NSLog(@"-----------end"); UIImage *image = [UIImage imageWithData:data]; // 2.回到主线程显示图片 // [self.imaV performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO modes:nil]; //[self performSelectorOnMainThread:@selector(settingImage:) withObject:nil waitUntilDone:NO]; [self.imaV performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO]; } /** * 显示图片:主线程 * * @param image image */ - (void)settingImage:(UIImage *)image { self.imaV.image = image; } @end
小结:主线程与子线程之间的通信
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。