您的位置:威尼斯官方网站 > 威尼斯官方网站 > 03-队列组了然 1

03-队列组了然 1

发布时间:2019-08-11 03:52编辑:威尼斯官方网站浏览(163)

    03-队列组 (了解),03-队列组了解

      1 #import "XZViewController.h"
      2 
      3 // 1.分别下载2张图片:大图片、LOGO
      4 // 2.合并2张图片
      5 // 3.显示到一个imageView身上
      6 
      7 @interface XZViewController ()
      8 @property (weak, nonatomic) IBOutlet UIImageView *imageView;
      9 @property (nonatomic, strong) UIImage *image1;
     10 @property (nonatomic, strong) UIImage *image2;
     11 @end
     12 
     13 @implementation XZViewController
     14 
     15 - (void)viewDidLoad
     16 {
     17     [super viewDidLoad];
     18     
     19 }
     20 
     21 // 2D绘图  Quartz2D
     22 // 合并图片 -- 水印
     23 
     24 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
     25 {   // 方法三
     26     // 1.队列组
     27     dispatch_group_t group = dispatch_group_create();
     28     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
     29     
     30     // 2.下载图片1
     31     __block UIImage *image1 = nil;
     32     dispatch_group_async(group, queue, ^{
     33         NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
     34         NSData *data1 = [NSData dataWithContentsOfURL:url1];
     35         image1 = [UIImage imageWithData:data1];
     36     });
     37     
     38     // 3.下载图片2
     39     __block UIImage *image2 = nil;
     40     dispatch_group_async(group, queue, ^{
     41         NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
     42         NSData *data2 = [NSData dataWithContentsOfURL:url2];
     43         image2 = [UIImage imageWithData:data2];
     44     });
     45     
     46     // 4.合并图片 (保证执行完组里面的所有任务之后,再执行notify函数里面的block)
     47     dispatch_group_notify(group, queue, ^{
     48         // 开启一个位图上下文
     49         UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0);
     50         
     51         // 绘制第1张图片
     52         CGFloat image1W = image1.size.width;
     53         CGFloat image1H = image1.size.height;
     54         [image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
     55         
     56         // 绘制第2张图片
     57         CGFloat image2W = image2.size.width * 0.5;
     58         CGFloat image2H = image2.size.height * 0.5;
     59         CGFloat image2Y = image1H - image2H;
     60         [image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
     61         
     62         // 得到上下文中的图片
     63         UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
     64         
     65         // 结束上下文
     66         UIGraphicsEndImageContext();
     67         
     68         // 5.回到主线程显示图片
     69         dispatch_async(dispatch_get_main_queue(), ^{
     70             self.imageView.image = fullImage;
     71         });
     72     });
     73 }
     74 /** 方法二  */
     75 - (void)test2
     76 {
     77     // 异步下载
     78     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     79         // 1.下载第1张
     80         NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
     81         NSData *data1 = [NSData dataWithContentsOfURL:url1];
     82         self.image1 = [UIImage imageWithData:data1];
     83         
     84         [self bindImages];
     85     });
     86     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
     87         // 2.下载第2张
     88         NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
     89         NSData *data2 = [NSData dataWithContentsOfURL:url2];
     90         self.image2 = [UIImage imageWithData:data2];
     91         
     92         [self bindImages];
     93     });
     94 }
     95 
     96 - (void)bindImages
     97 {
     98     if (self.image1 == nil || self.image2 == nil) return;
     99     
    100     // 3.合并图片
    101     // 开启一个位图上下文
    102     UIGraphicsBeginImageContextWithOptions(self.image1.size, NO, 0.0);
    103     
    104     // 绘制第1张图片
    105     CGFloat image1W = self.image1.size.width;
    106     CGFloat image1H = self.image1.size.height;
    107     [self.image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
    108     
    109     // 绘制第2张图片
    110     CGFloat image2W = self.image2.size.width * 0.5;
    111     CGFloat image2H = self.image2.size.height * 0.5;
    112     CGFloat image2Y = image1H - image2H;
    113     [self.image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
    114     
    115     // 得到上下文中的图片
    116     UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    117     
    118     // 结束上下文
    119     UIGraphicsEndImageContext();
    120     
    121     // 4.回到主线程显示图片
    122     dispatch_async(dispatch_get_main_queue(), ^{
    123         self.imageView.image = fullImage;
    124     });
    125 }
    126 /** 方法一 */
    127 - (void)test1
    128 {
    129     // 异步下载
    130     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    131         // 1.下载第1张
    132         NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"];
    133         NSData *data1 = [NSData dataWithContentsOfURL:url1];
    134         UIImage *image1 = [UIImage imageWithData:data1];
    135         
    136         // 2.下载第2张
    137         NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"];
    138         NSData *data2 = [NSData dataWithContentsOfURL:url2];
    139         UIImage *image2 = [UIImage imageWithData:data2];
    140         
    141         // 3.合并图片
    142         // 开启一个位图上下文
    143         UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0);
    144         
    145         // 绘制第1张图片
    146         CGFloat image1W = image1.size.width;
    147         CGFloat image1H = image1.size.height;
    148         [image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
    149         
    150         // 绘制第2张图片
    151         CGFloat image2W = image2.size.width * 0.5;
    152         CGFloat image2H = image2.size.height * 0.5;
    153         CGFloat image2Y = image1H - image2H;
    154         [image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
    155         
    156         // 得到上下文中的图片
    157         UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
    158         
    159         // 结束上下文
    160         UIGraphicsEndImageContext();
    161         
    162         // 4.回到主线程显示图片
    163         dispatch_async(dispatch_get_main_queue(), ^{
    164             self.imageView.image = fullImage;
    165         });
    166     });
    167 }
    168 
    169 @end
    

     

    (了解),03-队列组了解 1 #import " XZViewController.h " 2 3 // 1.分别下载2张图片:大图片、LOGO 4 // 2.合并2张图片 5 // 3.显示到一个imageVi...

    本文由威尼斯官方网站发布于威尼斯官方网站,转载请注明出处:03-队列组了然 1

    关键词:

上一篇:进入控制面板

下一篇:没有了