码迷,mamicode.com
首页 > 移动开发 > 详细

ios多线程GCD group

时间:2016-01-25 16:45:45      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

  1 #import "HMViewController.h"
  2 
  3 @interface HMViewController ()
  4 @property (weak, nonatomic) IBOutlet UIImageView *imageView1;
  5 @property (weak, nonatomic) IBOutlet UIImageView *imageView2;
  6 @property (weak, nonatomic) IBOutlet UIImageView *bigImageView;
  7 @end
  8 
  9 @implementation HMViewController
 10 
 11 - (void)viewDidLoad
 12 {
 13     [super viewDidLoad];
 14     // Do any additional setup after loading the view, typically from a nib.
 15     
 16     // 图片1: http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg
 17     // 图片2: http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg
 18     /**
 19      1.下载图片1和图片2
 20      
 21      2.将图片1和图片2合并成一张图片后显示到imageView上
 22      
 23      思考:
 24      * 下载图片 : 子线程
 25      * 等2张图片都下载完毕后, 才回到主线程
 26      */
 27 }
 28 
 29 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 30 {
 31     // 创建一个组
 32     dispatch_group_t group = dispatch_group_create();
 33     
 34     // 开启一个任务下载图片1
 35     __block UIImage *image1 = nil;
 36     dispatch_group_async(group, global_queue, ^{
 37         image1 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
 38     });
 39     
 40     // 开启一个任务下载图片2
 41     __block UIImage *image2 = nil;
 42     dispatch_group_async(group, global_queue, ^{
 43         image2 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg"];
 44     });
 45     
 46     // 同时执行下载图片1\下载图片2操作
 47     
 48     // 等group中的所有任务都执行完毕, 再回到主线程执行其他操作
 49     dispatch_group_notify(group, main_queue, ^{
 50         self.imageView1.image = image1;
 51         self.imageView2.image = image2;
 52         
 53         // 合并
 54         UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
 55         [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
 56         [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
 57         self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext();
 58         // 关闭上下文
 59         UIGraphicsEndImageContext();
 60     });
 61 //    if (self.log == NO) {
 62 //        NSLog(@"-------touchesBegan");
 63 //        self.log = YES;
 64 //    }
 65 //    static dispatch_once_t onceToken;
 66 //    dispatch_once(&onceToken, ^{
 67 //        NSLog(@"-------touchesBegan");
 68 //    });
 69 }
 70 
 71 - (void)downlaod2image
 72 {
 73     dispatch_async(global_queue, ^{
 74         NSLog(@"下载图片---%@", [NSThread currentThread]);
 75         
 76         // 下载图片1
 77         UIImage *image1 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
 78         NSLog(@"下载完图片1---%@", [NSThread currentThread]);
 79         // 下载图片2
 80         UIImage *image2 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
 81         NSLog(@"下载完图片2---%@", [NSThread currentThread]);
 82         
 83         dispatch_async(main_queue, ^{
 84             NSLog(@"显示图片---%@", [NSThread currentThread]);
 85             
 86             self.imageView1.image = image1;
 87             self.imageView2.image = image2;
 88             
 89             // 合并
 90             UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
 91             [image1 drawInRect:CGRectMake(0, 0, 100, 100)];
 92             [image2 drawInRect:CGRectMake(100, 0, 100, 100)];
 93             self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext();
 94             // 关闭上下文
 95             UIGraphicsEndImageContext();
 96         });
 97     });
 98 }
 99 
100 - (UIImage *)imageWithURL:(NSString *)urlStr
101 {
102     NSURL *url = [NSURL URLWithString:urlStr];
103     NSData *data = [NSData dataWithContentsOfURL:url]; // 这行会比较耗时
104     return [UIImage imageWithData:data];
105 }
106 
107 - (void)delay
108 {
109     //    NSLog(@"----touchesBegan----%@", [NSThread currentThread]);
110     
111     //    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
112     //        [self performSelector:@selector(run) withObject:nil afterDelay:2.0];
113     //    });
114     // 1.全局并发队列
115     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
116     
117     // 2.计算任务执行的时间
118     dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));
119     
120     // 3.会在when这个时间点, 执行queue中的任务
121     dispatch_after(when, queue, ^{
122         NSLog(@"----run----%@", [NSThread currentThread]);
123     });
124 }
125 
126 @end

 

ios多线程GCD group

标签:

原文地址:http://www.cnblogs.com/PJHome/p/5157672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!