下載多張圖片,合成一張

需求:下載多張圖片,合成一張。

實現(xiàn)方法兩個:

    1. dispatch_group_t 線程組,使用并行隊列,執(zhí)行完畢觸發(fā)dispatch_group_notify(group, queue, ^{ }.
    1. 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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容