ios-调度组的使用

有种情况是我们要同时下载两张图片,开了两个线程去下载,每个线程下载一张图片,我们想让两张图片下载完成之后去合并就是把一张图片放到另一张图片里面当水印,这样的话我们就要用到调度组了,先等两个图片都先下载完,再去合并,废话不说,直接上代码

//调度组的基本使用,模拟异步下载歌曲
-(void)test3
{
    //创建组
    dispatch_group_t group=dispatch_group_create();
    //创建队列
    dispatch_queue_t queue=dispatch_get_global_queue(0, 0);
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第一个歌曲");
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第二个歌曲");
    });
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第三个歌曲");
    });
    //当三个异步任务都执行完毕之后,再去执行
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"结束 %@",[NSThread currentThread]);
    });
}
调度组的基本原理

//调度组的基本原理
-(void)test4
{
    //创建组
    dispatch_group_t group=dispatch_group_create();
    //创建队列
    dispatch_queue_t queue=dispatch_get_global_queue(0, 0);
    //可以理解为计数的操作,调用 dispatch_group_enter(group);标记着一个代码被加入了group,leave标记着组中的一个任务的结束。
    dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第一个歌曲");
        dispatch_group_leave(group);
    });
     dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第二个歌曲");
        dispatch_group_leave(group);
    });
     dispatch_group_enter(group);
    dispatch_group_async(group, queue, ^{
        NSLog(@"下载第三个歌曲");
        dispatch_group_leave(group);
    });
    //等待组里面的任务都执行完毕之后,才会去执行
    dispatch_group_notify(group, queue, ^{
        NSLog(@"----");
    });
    //等待组中的任务都执行完之后,再去继续执行后面的代码,会阻塞当前线程
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
    NSLog(@"lll");

}

调度组的应用

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //队列组
    dispatch_group_t group=dispatch_group_create();
    dispatch_queue_t queue =dispatch_get_global_queue(0, 0);
    //下载图片1
    __block UIImage * image1=nil;
    dispatch_group_async(group, queue, ^{
        NSURL * url=[NSURL URLWithString:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1503982959&di=7a62
c15e0bcb53a8b16475905035bc38&imgtype=jpg&er=1&src=http%3A%2F%2Fuus-img1.android.d.cn%2Fcontent_pic%2F201509%2Fbehpic%2Ficon%2F558%
   2F2-54558%2Ficon-google.png"];
        NSData * data1=[NSData dataWithContentsOfURL:url];
        image1=[UIImage imageWithData:data1];
    });
    //下载图片 2
    __block UIImage * image2=nil;
    dispatch_group_async(group, queue, ^{
        NSURL * url=[NSURL URLWithString:@"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"];
        NSData * data2=[NSData dataWithContentsOfURL:url];
        image2=[UIImage imageWithData:data2];
    });
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //开启图形上下文
        UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0);
        //把图片画上去
        [image1 drawInRect:CGRectMake(0, 0,image1.size.width, image1.size.height)];
        //绘制第二张图片
        [image2 drawInRect:CGRectMake(0, 0, image2.size.width*0.2, image2.size.width*0.2)];
        //得到图形上下文中的图片
        UIImage * image=UIGraphicsGetImageFromCurrentImageContext();
        //关闭图形上下文
        UIGraphicsEndImageContext();
        //设置图片
        self.imageView.image=image;
    });
}



你可能感兴趣的:(ios-开发)