IOS第二天多线程-03对列组合并图片

*********

// 2D绘图  Quartz2D
// 合并图片 -- 水印

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 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://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.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://su.bdimg.com/static/superplus/img/logo_white_ee663702.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;
        });
    });
}
时间: 2024-08-03 10:12:50

IOS第二天多线程-03对列组合并图片的相关文章

IOS第二天多线程-03线程间通信

**** #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loadi

IOS第二天多线程-05GCD队列的使用

************** // // HMViewController.m // 08-GCD02-队列的使用(了解) // // Created by apple on 14-9-15. // Copyright (c) 2014年 heima. All rights reserved. // // dispatch_sync : 同步,不具备开启线程的能力 // dispatch_async : 异步,具备开启线程的能力 // 并发队列 :多个任务可以同时执行 // 串行队列 :一个任务

IOS第二天多线程-05NSOperationQueue 暂停,和恢复队列任务

*********** #import "HMViewController.h" @interface HMViewController () <UITableViewDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; } -

IOS第二天多线程-04GCD通信

**** #define HMGlobalQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) #define HMMainQueue dispatch_get_main_queue() #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIButton *but

IOS第二天多线程-02NSThread基本使用

**** #import "HMViewController.h" @interface HMViewController () @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)download:(NSStri

IOS第二天多线程-04简化单例模式

******HMSingleton-ARC.h // .h文件 #define HMSingletonH(name) + (instancetype)shared##name; // .m文件 #define HMSingletonM(name) static id _instance; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken,

IOS第二天多线程-01-延时执行

**********延时执行 #import "HMViewController.h" @interface HMViewController () @end @implementation HMViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)touche

IOS开发之多线程 -- GCD的方方面面

前言:这篇GCD的博文是本人阅读了很多海内外大神的关于GCD的文章,以及结合之前自己对GCD的粗浅的认识,然后取其精华,去其槽粕,综合起来的笔记,而且是尽可能的以通熟易懂的并且是正确的理论论述方式呈现给读者,同时也是讲大神博客中有的深涩的理论理解的通熟易懂转述给读者,已经是尽可能的让读者深入理解和掌握多线程的知识以及GCD的使用技术.最后的附录中,我将会给出所有本人阅读的大神写的关于多线程或者是GCD的文章链接,大家感兴趣的,可以去参考和学习.也许,看我的这篇就够了,因为我就是参考他们的,嘻嘻.

iOS 37班多线程,socket,HTTP,ASIHttpRequest,等面试题总结集合

iOS 37班多线程,socket,HTTP,ASIHttpRequest,等面试题总结集合 博客分类: ASIDownloadCache 设置下载缓存 它对Get请求的响应数据进行缓存(被缓存的数据必需是成功的200请求): [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 当设置缓存策略后,所有的请求都被自动的缓存起来.另外,如果仅仅希望某次请求使用缓存操作,也可以这样使用: ASIHTTPRequest *re