NSThread
//第一种创建线程的方式:alloc init.
//特点:需要手动开启线程,可以拿到线程对象进行详细设置
//创建线程
/*
第一个参数:目标对象
第二个参数:选择器,线程启动要调用哪个方法
第三个参数:前面方法要接收的参数(最多只能接收一个参数,没有则传nil)
*/
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(threadAction1) object:nil];
//启动线程
[thread start];
//设置线程的名称
thread.name = @"子线程";
//设置线程的优先级,注意线程优先级的取值范围为0.0~1.0之间,1.0表示线程的优先级最高,如果不设置该值,那么理想状态下默认为0.5
thread.threadPriority = 1.0;
//第二种创建线程的方式:分离出一条子线程
//特点:自动启动线程,无法对线程进行更详细的设置
/*
第一个参数:线程启动调用的方法
第二个参数:目标对象
第三个参数:传递给调用方法的参数
*/
[NSThread detachNewThreadWithBlock:^{
NSLog(@"%@",[NSThread currentThread]);
}];
//第三种创建线程的方式:后台线程
//特点:自动启动县城,无法进行更详细设置
[self performSelectorInBackground:@selector(threadAction1) withObject:nil];
GCD
GCD 详解:http://www.jianshu.com/p/d1f9b2a7b0a8
//0.获取一个全局的队列
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//1.先开启一个线程,把下载图片的操作放在子线程中处理
dispatch_async(queue, ^{
//2.下载图片
NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSLog(@"下载操作所在的线程--%@",[NSThread currentThread]);
//3.回到主线程刷新UI
dispatch_sync(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithData:imageData];
NSLog(@"刷新UI---%@",[NSThread currentThread]);
});
});
//1.创建队列
/*
第一个参数:C语言的字符串,标签
第二个参数:队列的类型
DISPATCH_QUEUE_CONCURRENT:并发
DISPATCH_QUEUE_SERIAL:串行
*/
dispatch_queue_t serialQueue = dispatch_queue_create("@download", DISPATCH_QUEUE_SERIAL);
dispatch_barrier_async(serialQueue, ^{
});
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentQueue, ^{
});
NSOperation
__weak typeof(self) weakSelf = self;
//创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
NSURL *url = [NSURL URLWithString: @"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1504585897&di=7a7b3940fd17b93e2d9a5805cc4e0ccb&src=http://img.zcool.cn/community/01eb6a5542979d0000019ae97e45d6.jpg"];
NSBlockOperation *downOperation = [NSBlockOperation blockOperationWithBlock:^{
NSData *imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
weakSelf.imageView.image = [UIImage imageWithData:imageData];
}];
}];
[queue addOperation:downOperation];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//下载图片1
NSBlockOperation *download1 = [NSBlockOperation blockOperationWithBlock:^{
self.image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1504585897&di=7a7b3940fd17b93e2d9a5805cc4e0ccb&src=http://img.zcool.cn/community/01eb6a5542979d0000019ae97e45d6.jpg"]]];
}];
//下载图片2
NSBlockOperation *download2 = [NSBlockOperation blockOperationWithBlock:^{
self.image2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1504585897&di=7a7b3940fd17b93e2d9a5805cc4e0ccb&src=http://img.zcool.cn/community/01eb6a5542979d0000019ae97e45d6.jpg"]]];
}];
//合并图片
NSBlockOperation *add = [NSBlockOperation blockOperationWithBlock:^{
//开启图形上下文
UIGraphicsBeginImageContext(CGSizeMake(200, 200));
//画image1
[self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];
[self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];
//根据图形上下文拿到图片数据
self.addImage = UIGraphicsGetImageFromCurrentImageContext();
//关闭图形上下文
UIGraphicsEndImageContext();
//回到主线程刷新UI
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
self.imageView.image = self.addImage;
}];
}];
//设置操作依赖
[add addDependency:download1];
[add addDependency:download2];
//添加操作到队列中执行
[queue addOperation:download1];
[queue addOperation:download2];
[queue addOperation:add];