zoukankan      html  css  js  c++  java
  • 队列组-合并图片-07-08-09-GCD

     1 //
     2 //  ViewController.m
     3 //  07-合并图片
     4 //
     5 //  Created by mac on 16/4/21.
     6 //  Copyright © 2016年 mac. All rights reserved.
     7 //
     8 //    NSURL *url = [NSURL URLWithString:@"http://h.hiphotos.baidu.com/image/pic/item/574e9258d109b3dedb168a8ec8bf6c81810a4cae.jpg"];
     9 //    NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
    10 
    11 #import "UIImageView+WebCache.h"
    12 #import "ViewController.h"
    13 
    14 @interface ViewController ()
    15 
    16 @end
    17 
    18 @implementation ViewController {
    19     
    20     UIImageView *imageView;
    21 }
    22 
    23 - (void)viewDidLoad {
    24     [super viewDidLoad];
    25 
    26     imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    27     imageView.backgroundColor = [UIColor cyanColor];
    28     
    29     [self.view addSubview:imageView];
    30 }
    31 
    32 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    33 
    34     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    35         
    36         //1. 下载第一张图片
    37         NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
    38         
    39         NSData *data = [NSData dataWithContentsOfURL:url];
    40         UIImage *image = [UIImage imageWithData:data];
    41         
    42         //2. 下载第二种图片
    43         NSURL *url2 = [NSURL URLWithString:@"http://www.jushuo.com/uploads/allimg/151204/5-1512040Z34a50.jpg"];
    44         
    45         NSData *data2 = [NSData dataWithContentsOfURL:url2];
    46         UIImage *image2 = [UIImage imageWithData:data2];
    47         
    48         
    49         //3. 合并 :
    50         //1)开启一个位图上下文
    51         UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    52         
    53         //2) 绘制第一张和第二张图
    54         [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    55         [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height * 0.5)];
    56         
    57         //3) 得到上下文中的图片
    58         UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    59         
    60         //4) 结束上下文
    61         UIGraphicsEndImageContext();
    62         
    63         
    64         //4. 主线程中加载UI
    65         dispatch_async(dispatch_get_main_queue(), ^{
    66            
    67             imageView.image = fullImage;
    68         });
    69 
    70     });
    71 }
    72 
    73 @end
     1 //
     2 //  ViewController.m
     3 //  08-队列组-GCD
     4 //
     5 //  Created by mac on 16/4/21.
     6 //  Copyright © 2016年 mac. All rights reserved.
     7 //
     8 
     9 #import "ViewController.h"
    10 
    11 @interface ViewController ()
    12 
    13 @end
    14 
    15 @implementation ViewController {
    16 
    17     UIImageView *imageView;
    18     UIImage *image;
    19     UIImage *image2;
    20 }
    21 
    22 - (void)viewDidLoad {
    23     [super viewDidLoad];
    24     
    25     imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    26     imageView.backgroundColor = [UIColor cyanColor];
    27     
    28     [self.view addSubview:imageView];
    29 }
    30 
    31 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    32     
    33     if (image || image2) return;
    34     
    35     NSLog(@"hahaha");
    36     
    37     [self downloadImage];
    38 }
    39 
    40 - (void)downloadImage {
    41     
    42         //1. 下载第一张图片
    43     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    44         
    45         NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
    46         NSData *data = [NSData dataWithContentsOfURL:url];
    47         image = [UIImage imageWithData:data];
    48         
    49         [self text];
    50     });
    51     
    52        //2. 下载第二张图片
    53     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    54         
    55         NSURL *url2 = [NSURL URLWithString:@"http://www.jushuo.com/uploads/allimg/151204/5-1512040Z34a50.jpg"];
    56         NSData *data2 = [NSData dataWithContentsOfURL:url2];
    57         image2 = [UIImage imageWithData:data2];
    58         
    59         [self text];
    60     });
    61 }
    62 
    63 - (void)text {
    64     
    65     if (image == nil || image2 == nil) return;
    66         
    67     //3. 合并 :
    68 
    69     //1)开启一个位图上下文
    70         UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    71         
    72         //2) 绘制第一张和第二张图
    73         [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    74         [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height * 0.5)];
    75         
    76         //3) 得到上下文中的图片
    77         UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    78         
    79         //4) 结束上下文
    80         UIGraphicsEndImageContext();
    81         
    82         //4. 主线程中加载UI
    83         dispatch_async(dispatch_get_main_queue(), ^{
    84             
    85             imageView.image = fullImage;
    86         });
    87 }
    88 
    89 @end
     1 //
     2 //  NewViewController.m
     3 //  08-队列组-GCD
     4 //
     5 //  Created by mac on 16/4/21.
     6 //  Copyright © 2016年 mac. All rights reserved.
     7 //
     8 
     9 #import "NewViewController.h"
    10 
    11 @interface NewViewController ()
    12 
    13 @end
    14 
    15 @implementation NewViewController {
    16     
    17         UIImageView *imageView;
    18 }
    19 
    20 - (void)viewDidLoad {
    21     [super viewDidLoad];
    22     
    23     imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    24     imageView.backgroundColor = [UIColor cyanColor];
    25     
    26     [self.view addSubview:imageView];
    27 }
    28 
    29 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    30     
    31     [self downloadImage];
    32     NSLog(@"downloadImage");
    33 }
    34 
    35 /**
    36  *  队列组(dispatch_group_t )(dispatch_group_notify)
    37  */
    38 - (void)downloadImage {
    39     
    40     //1. 队列组
    41     dispatch_group_t group = dispatch_group_create();
    42     
    43     //2. 队列
    44     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    45     
    46     //3. 下载第一张图片
    47     __block UIImage *image = nil;
    48     dispatch_group_async(group, queue, ^{
    49         
    50         NSURL *url = [NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
    51         NSData *data = [NSData dataWithContentsOfURL:url];
    52         image = [UIImage imageWithData:data];
    53     });
    54     
    55     //4. 下载第二张图片
    56     __block UIImage *image2 = nil;
    57     dispatch_group_async(group, queue, ^{
    58         
    59         NSURL *url2 = [NSURL URLWithString:@"http://www.jushuo.com/uploads/allimg/151204/5-1512040Z34a50.jpg"];
    60         NSData *data2 = [NSData dataWithContentsOfURL:url2];
    61         image2 = [UIImage imageWithData:data2];
    62     });
    63     
    64     //5. 合并图片(保证执行完组里面的所有任务之后,在执行notify里面的block )
    65     dispatch_group_notify(group, queue, ^{
    66         
    67         //1)开启一个位图上下文
    68         UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    69         
    70         //2) 绘制第一张和第二张图
    71         [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
    72         [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height * 0.5)];
    73         
    74         //3) 得到上下文中的图片
    75         UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    76         
    77         //4) 结束上下文
    78         UIGraphicsEndImageContext();
    79         
    80         //5. 回到主线程中加载UI
    81         dispatch_async(dispatch_get_main_queue(), ^{
    82             
    83             imageView.image = fullImage;
    84         });
    85     });
    86 
    87 }
    88 
    89 @end
    时光见证了成长,还很无知,我想一点点幼稚转为有知!
  • 相关阅读:
    [chrome]click事件会触发mouseleave
    鼠标的指针状态 以及 事件禁用
    CSS3 线性渐变(linear-gradient)
    css 的函数 calc() 、linear-gradient()、、、
    1.闰年的计算方法。 2.某一月的周数
    moment.js 使用方法总结
    Echarts 版本查看
    如何使用 onscroll / scrollTo() / scrollBy()
    水平居中、垂直居中
    【LeetCode】22. Generate Parentheses (I thought I know Python...)
  • 原文地址:https://www.cnblogs.com/foreveriOS/p/5417310.html
Copyright © 2011-2022 走看看