zoukankan      html  css  js  c++  java
  • iOS-GCD 队列组的使用

        // 1.队列组
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        // 2.下载图片1
        __block UIImage *image1 = nil;
        dispatch_group_async(group, queue, ^{
            NSURL *url1 = [NSURL URLWithString:@"http://111.jpg"];
            NSData *data1 = [NSData dataWithContentsOfURL:url1];
            image1 = [UIImage imageWithData:data1];
        });
        
        // 3.下载图片2
        __block UIImage *image2 = nil;
        dispatch_group_async(group, queue, ^{
            NSURL *url2 = [NSURL URLWithString:@"http://222.png"];
            NSData *data2 = [NSData dataWithContentsOfURL:url2];
            image2 = [UIImage imageWithData:data2];
        });
        
        // 4.合并图片 (保证执行完组里面的所有任务之后,再执行notify函数里面的block)
        dispatch_group_notify(group, queue, ^{
            // 开启一个位图上下文
            UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0);
            
            // 绘制第1张图片
            CGFloat image1W = image1.size.width;
            CGFloat image1H = image1.size.height;
            [image1 drawInRect:CGRectMake(0, 0, image1W, image1H)];
            
            // 绘制第2张图片
            CGFloat image2W = image2.size.width * 0.5;
            CGFloat image2H = image2.size.height * 0.5;
            CGFloat image2Y = image1H - image2H;
            [image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)];
            
            // 得到上下文中的图片
            UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
            
            // 结束上下文
            UIGraphicsEndImageContext();
            
            // 5.回到主线程显示图片
            dispatch_async(dispatch_get_main_queue(), ^{
                self.imageView.image = fullImage;
            });
        });
  • 相关阅读:
    bzoj 4911: [Sdoi2017]切树游戏
    bzoj 2654: tree
    bzoj 3240: [Noi2013]矩阵游戏
    有标号的DAG计数 III
    有标号的DAG计数 II
    bzoj 3512: DZY Loves Math IV
    bzoj 4480: [Jsoi2013]快乐的jyy
    bzoj 5323: [Jxoi2018]游戏
    codeforces412A
    7.6 T1 深度优先搜索(dfs)
  • 原文地址:https://www.cnblogs.com/DarbyCJ/p/4641780.html
Copyright © 2011-2022 走看看