多线程 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]);
        //
    });
     
    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.若想要快速高效的实现上述需求,可以考虑用队列组

 
 
原文地址:https://www.cnblogs.com/Cheetah-yang/p/4664153.html