需求:下载多张图片,合成一张。
实现方法两个:
-
dispatch_group_t
线程组,使用并行队列,执行完毕触发dispatch_group_notify(group, queue, ^{ }
.
-
-
NSOperationQueue
、NSBlockOperation
加入到执行队列,添加依赖,或等待。
-
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIImage *imageOne;
@property (nonatomic, strong) UIImage *imageTwo;
@property (nonatomic, weak) UILabel *textLabel;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self groupMethod];
// [self operationQueueMethod];
}
- (void) groupMethod {
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 450, 0, 0)];
textLabel.text = @"下载图片";
[textLabel sizeToFit];
[self.view addSubview:textLabel];
self.textLabel = textLabel;
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create("cn.gcd-group.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
NSLog(@"正在下载第一张图片");
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://blog.ibireme.com/wp-content/uploads/2015/10/memory_cache_bench_result.png"]];
NSLog(@"第一张图片下载完毕");
self.imageOne = [UIImage imageWithData:data];
});
dispatch_group_async(group, queue, ^{
NSLog(@"正在下载第二张图片");
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://blog.ibireme.com/wp-content/uploads/2015/10/disk_cache_bench_result.png"]];
NSLog(@"第二张图片下载完毕");
self.imageTwo = [UIImage imageWithData:data];
});
dispatch_group_notify(group, queue, ^{
UIGraphicsBeginImageContext(CGSizeMake(300, 300));
[self.imageOne drawInRect:CGRectMake(0, 100, 150, 300)];
[self.imageTwo drawInRect:CGRectMake(180, 100, 150, 300)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
[self.view addSubview:imageView];
self.textLabel.text = @"图片合成";
});
});
NSLog(@"主线程正常执行");
// Do any additional setup after loading the view, typically from a nib.
}
- (void)operationQueueMethod {
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 450, 0, 0)];
textLabel.text = @"正在下载图片";
[textLabel sizeToFit];
[self.view addSubview:textLabel];
self.textLabel = textLabel;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 2;
// 第一张图片
NSBlockOperation *op_one = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"正在下载第一张图片");
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://blog.ibireme.com/wp-content/uploads/2015/10/memory_cache_bench_result.png"]];
NSLog(@"第一张图片下载完毕");
self.imageOne = [UIImage imageWithData:data];
}];
// 第二张图片
NSBlockOperation *op_two = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"正在下载第二张图片");
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://blog.ibireme.com/wp-content/uploads/2015/10/disk_cache_bench_result.png"]];
NSLog(@"第二张图片下载完毕");
self.imageTwo = [UIImage imageWithData:data];
}];
// 添加到队列执行
// [queue addOperation:op1];
// [queue addOperation:op2];
// NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
//
// UIGraphicsBeginImageContext(CGSizeMake(300, 400));
//
// [self.imageOne drawInRect:CGRectMake(0, 0, 150, 400)];
// [self.imageTwo drawInRect:CGRectMake(150, 0, 150, 400)];
//
// UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
// UIGraphicsEndImageContext();
//
// dispatch_async(dispatch_get_main_queue(), ^{
// UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
// [self.view addSubview:imageView];
// self.textLabel.text = @"图片合并完毕";
// });
// }];
// [operation addDependency: op1];
// [operation addDependency: op2];
// [queue addOperation:operation];/// 不会阻塞主线程
[queue addOperations:@[op_one, op_two] waitUntilFinished:true];/// 会阻塞主线程
[queue addOperationWithBlock:^{
UIGraphicsBeginImageContext(CGSizeMake(300, 300));
[self.imageOne drawInRect:CGRectMake(0, 100, 150, 300)];
[self.imageTwo drawInRect:CGRectMake(180, 100, 150, 300)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imageView = [[UIImageView alloc] initWithImage:newImage];
[self.view addSubview:imageView];
self.textLabel.text = @"图片合成";
});
}];
NSLog(@"主线程会等到队列执行完,即会阻塞主线程");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end