需求:下載多張圖片,合成一張。
實現(xiàn)方法兩個:
-
dispatch_group_t線程組,使用并行隊列,執(zhí)行完畢觸發(fā)dispatch_group_notify(group, queue, ^{ }.
-
-
NSOperationQueue、NSBlockOperation加入到執(zhí)行隊列,添加依賴,或等待。
-
#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(@"主線程正常執(zhí)行");
// 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];
}];
// 添加到隊列執(zhí)行
// [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(@"主線程會等到隊列執(zhí)行完,即會阻塞主線程");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end