线程与下载
#pragma mark 创建新线程3
- (void)createThread3 {
// 隐式创建一个新线程执行self的run:方法
// 创建完毕后马上启动线程
[self performSelectorInBackground:@selector(run:) withObject:@"mj"];
}
#pragma mark 创建新线程2
- (void)createThread2 {
// 隐式创建一个新线程执行self的run:方法
// 创建完毕后马上启动线程
[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"mj"];
}
#pragma mark 创建新线程
- (void)createThread {
NSThread *thread = [[[NSThread alloc] initWithTarget:self selector:@selector(run:) object:@"mj"] autorelease];
NSLog(@"thread-%@", thread);
// 启动线程
[thread start];
}
#pragma mark 下载图片
- (void)download {
[self performSelectorInBackground:@selector(downloadImage) withObject:nil];
}
#pragma mark 在后台线程下载图片
- (void)downloadImage {
NSLog(@"%@", [NSThread currentThread]);
NSString *url = @"http://f.hiphotos.baidu.com/album/w%3D2048/sign=f2f363781e30e924cfa49b3178306f06/9922720e0cf3d7ca8e304106f31fbe096a63a9f8.jpg";
NSURL *URL = [NSURL URLWithString:url];
NSData *data = [NSData dataWithContentsOfURL:URL];
UIImage *image = [UIImage imageWithData:data];
// 在主线程调用self.imageView的setImage:方法
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
}