异步下载多张图片并拼接

- (void)downloadImg
{
    //创建队列组
    dispatch_group_t group = dispatch_group_create();
    
    //创建队列(并发队列)
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
   
    //下载图片1
    dispatch_group_async(group, queue, ^{
        //1.获取url地址
        NSURL *url = [NSURL URLWithString:@"http://i0.hdslb.com/bfs/archive/3ca9c1328e04e376cf1f367011a1e6d3cabd16bd.jpg"];
        //2.下载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        //3.把二进制数据转换成图片
        self.image1 = [UIImage imageWithData:data];
    });
    
    //下载图片2
    dispatch_group_async(group, queue, ^{
        //1.获取url地址
        NSURL *url = [NSURL URLWithString:@"http://ww4.sinaimg.cn/large/006d57JQjw1f7kumdczb9j30c807faap.jpg"];
        //2.下载图片
        NSData *data = [NSData dataWithContentsOfURL:url];
        //3.把二进制数据转换成图片
        self.image2 = [UIImage imageWithData:data];
    });
    
    //合成
    dispatch_group_notify(group, queue, ^{
        //开启图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(200, 200));
        
        //画图1
        [self.image1 drawInRect:CGRectMake(0, 0, 200, 100)];
        
        //画图2
        [self.image2 drawInRect:CGRectMake(0, 100, 200, 100)];
        
        //根据图形上下文获取图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        //关闭上下文
        UIGraphicsEndImageContext();
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = image;
            NSLog(@"%@--刷新UI", [NSThread currentThread]);
        });
    });
}

你可能感兴趣的:(异步下载多张图片并拼接)