zoukankan      html  css  js  c++  java
  • 多线程 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.若想要快速高效的实现上述需求,可以考虑用队列组

     
     
  • 相关阅读:
    小程序用户拒绝授权地理位置的处理办法
    云开发小程序数据库权限有限,通过云函数修改数据库评论信息
    小程序仿照微信朋友圈点击评论键盘输入
    小程序wx.previewImage查看图片再次点击返回时重新加载页面问题
    js手机端判断滑动还是点击
    Proxy
    Reflect.has检测对象是否拥有某个属性
    简单的axios请求返回数据解构赋值
    为windows terminal 配置 conda
    git clone 遇到问题:fatal: unable to access 'https://github.comxxxxxxxxxxx':
  • 原文地址:https://www.cnblogs.com/Cheetah-yang/p/4664153.html
Copyright © 2011-2022 走看看