zoukankan      html  css  js  c++  java
  • iOS多线程开发小demo7 GCD队列组

    //  DYFViewController.m
    //  623-08-队列组
    //
    //  Created by dyf on 14-6-23.
    //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
    //
    
    #import "DYFViewController.h"
    
    @interface DYFViewController ()
    @property (weak, nonatomic) IBOutlet UIImageView *iconV1;
    @property (weak, nonatomic) IBOutlet UIImageView *iconV2;
    @property (weak, nonatomic) IBOutlet UIImageView *bigIconV;
    
    @end
    
    @implementation DYFViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%@", [NSThread currentThread]);
        dispatch_group_t group = dispatch_group_create();
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        
        __block UIImage *icon1 = nil;
        dispatch_group_async(group, queue, ^{
            NSLog(@"%@", [NSThread currentThread]);
            //
            icon1 = [self imageWithURL:@"http://image.cache.xiu8.com/live/125/125/997729.jpg"];
            
        });
        __block UIImage *icon2 = nil;
        dispatch_group_async(group, queue, ^{
            NSLog(@"%@", [NSThread currentThread]);
            //
            icon2 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg"];
        });
        
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            NSLog(@"%@", [NSThread currentThread]);
            //
            self.iconV1.image = icon1;
            self.iconV2.image = icon2;
            
            UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0);
            [icon1 drawInRect:CGRectMake(0, 0, 100, 100)];
            [icon2 drawInRect:CGRectMake(100, 0, 100, 100)];
            self.bigIconV.image = UIGraphicsGetImageFromCurrentImageContext();
            
            UIGraphicsEndImageContext();
        });
    }
    
    - (UIImage *)imageWithURL:(NSString *)iconPath
    {
        NSLog(@"%@", [NSThread currentThread]);
        NSURL *url = [NSURL URLWithString:iconPath];
        NSData *data = [NSData dataWithContentsOfURL:url];
        return [UIImage imageWithData:data];
    }
    
    @end
    

     小结:

    ------------队列组------

    1.有这么一种需求

    ·首先:分别异步执行2个耗时的操作

    ·其次:等2各异步操作都执行完毕后,再回到主线程执行操作

    2.若想要快速高效的实现上述需求,可以考虑用队列组

  • 相关阅读:
    golang删除数组某个元素
    golang用通道实现信号量,控制并发个数
    什么是ScaleIO中的forwards rebuild和backwards rebuild?
    SQL Server中的database checkpoint
    如何将thick provision lazy zeroed的VMDK文件转换为thick provision eager zeroed?
    LoadTestAgentResultsLateException in VS2010
    SQL Server Instance无法启动了, 因为TempDB所在的分区没有了, 怎么办?
    VMware vCenter中, 如何辩认虚机上Raw Device Mapping过了的一块物理磁盘?
    SQL Server AlwaysOn Setup Step-By-Step Guide
    TPC-E在populate测试Database时需要注意的一些事项
  • 原文地址:https://www.cnblogs.com/dyf520/p/3805310.html
Copyright © 2011-2022 走看看