美文网首页
GCD 队列组 常用函数

GCD 队列组 常用函数

作者: solozyx | 来源:发表于2016-08-09 17:20 被阅读18次

队列组

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
/** 图片1 */
@property (nonatomic, strong) UIImage *image1;
/** 图片2 */
@property (nonatomic, strong) UIImage *image2;
- (void)group{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    // 创建一个队列组
    dispatch_group_t group = dispatch_group_create();
    
    // 1.下载图片1
    dispatch_group_async(group, queue, ^{
        NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        self.image1 = [UIImage imageWithData:data];
    });
    
    // 2.下载图片2
    dispatch_group_async(group, queue, ^{
        NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        self.image2 = [UIImage imageWithData:data];
    });
    
    // 3.将图片1、图片2合成一张新的图片
    dispatch_group_notify(group, queue, ^{
        // 开启新的图形上下文
        UIGraphicsBeginImageContext(CGSizeMake(100, 100));
        
        // 绘制图片
        [self.image1 drawInRect:CGRectMake(0, 0, 50, 100)];
        [self.image2 drawInRect:CGRectMake(50, 0, 50, 100)];
        
        // 取得上下文中的图片
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        
        // 结束上下文
        UIGraphicsEndImageContext();
        
        // 回到主线程显示图片
        dispatch_async(dispatch_get_main_queue(), ^{
            // 4.将新图片显示出来 
            self.imageView.image = image;
        });
    });
}

GCD : dispatch_barrier_async


- (void)barrier{ // 障碍
    dispatch_queue_t queue = dispatch_queue_create("com.solozyx.queue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        NSLog(@"1-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"2-----%@", [NSThread currentThread]);
    });
    
    dispatch_barrier_async(queue, ^{
        NSLog(@"barrier-----%@", [NSThread currentThread]);
    });
    
    dispatch_async(queue, ^{
        NSLog(@"3-----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"4-----%@", [NSThread currentThread]);
    });
}

//2016-08-09 17:22:45.066 09-掌握-GCD的其他常用函数[44042:694498] 1-----<NSThread: 0x7fc5f8c1a8e0>{number = 3, name = (null)}
//2016-08-09 17:22:45.066 09-掌握-GCD的其他常用函数[44042:694499] 2-----<NSThread: 0x7fc5f8e158c0>{number = 2, name = (null)}
//2016-08-09 17:22:45.067 09-掌握-GCD的其他常用函数[44042:694499] barrier-----<NSThread: 0x7fc5f8e158c0>{number = 2, name = (null)}
//2016-08-09 17:22:45.067 09-掌握-GCD的其他常用函数[44042:694499] 3-----<NSThread: 0x7fc5f8e158c0>{number = 2, name = (null)}
//2016-08-09 17:22:45.067 09-掌握-GCD的其他常用函数[44042:694498] 4-----<NSThread: 0x7fc5f8c1a8e0>{number = 3, name = (null)}

GCD : dispatch_apply

/**
 * 传统文件剪切
 */
- (void)moveFile{
    NSString *from = @"/Users/admin/Desktop/From";
    NSString *to = @"/Users/admin/Desktop/To";

    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subpaths = [mgr subpathsAtPath:from];

    for (NSString *subpath in subpaths) {
        NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
        NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil]; // 剪切
        });
    }
}
/**
 * 快速迭代
 */
- (void)apply{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    NSString *from = @"/Users/admin/Desktop/From";
    NSString *to = @"/Users/admin/Desktop/To";
    
    NSFileManager *mgr = [NSFileManager defaultManager];
    NSArray *subpaths = [mgr subpathsAtPath:from];
    
    dispatch_apply(subpaths.count, queue, ^(size_t index) {
        NSString *subpath = subpaths[index];
        NSString *fromFullpath = [from stringByAppendingPathComponent:subpath];
        NSString *toFullpath = [to stringByAppendingPathComponent:subpath];
        // 剪切
        [mgr moveItemAtPath:fromFullpath toPath:toFullpath error:nil];
        
        NSLog(@"%@---%@", [NSThread currentThread], subpath);
    });
}

//2016-08-09 17:32:19.958 GCD的常用函数[44565:705935] <NSThread: 0x7fb452402e20>{number = 1, name = main}---1.png
//2016-08-09 17:32:19.958 GCD的常用函数[44565:705968] <NSThread: 0x7fb45260e640>{number = 2, name = (null)}---2.png
//2016-08-09 17:32:19.958 GCD的常用函数[44565:705966] <NSThread: 0x7fb45249daa0>{number = 3, name = (null)}---3.png

GCD : 延时操作

/**
 * 延迟执行
 */
- (void)run{
    NSLog(@"run %@",[NSDate date]);
}

- (void)delay{
    [self run];
    [self performSelector:@selector(run) withObject:nil afterDelay:2.0];

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self run];
    });
    
    [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
}

//2016-08-09 17:43:33.840 GCD的常用函数[45098:713351] run 2016-08-09 09:43:33 +0000
//2016-08-09 17:43:35.846 GCD的常用函数[45098:713351] run 2016-08-09 09:43:35 +0000
//2016-08-09 17:43:36.842 GCD的常用函数[45098:713351] run 2016-08-09 09:43:36 +0000
//2016-08-09 17:43:38.226 GCD的常用函数[45098:713351] run 2016-08-09 09:43:38 +0000

GCD : dispatch_async_f

void download(void * data)
{
    printf("c : download");
}

- (void)excDownload{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async_f(queue, NULL, download);
}
// c : download

相关文章

  • 多线程02

    多线程2 GCD队列组: 基本使用: 常用函数 队列和组队列的区别 队列:封装任务,添加任务到队列 组队列:封装任...

  • GCD 队列组 常用函数

    队列组 GCD : dispatch_barrier_async GCD : dispatch_apply G...

  • GCD之简介

    1.多线程方案介绍 2.GCD中常用函数 同步执行 异步执行 3.GCD中常用队列 并发队列可以让多个任务并发(同...

  • iOS-GCD常用函数和栅栏函数

    GCD常用函数 GCD栅栏函数

  • iOS面试--GCD常见用法

    项目中常见的GCD用法有已下几种: 1.GCD栅栏函数2.GCD快速迭代(遍历)3.GCD队列组的使用 1.GCD...

  • iOS | 底层原理分析(二)

    一. 多线程 1.1 ios 多线程方案 1.2GCD的常用函数 1.3 GCD的队列 1.4 容易混淆的术语 1...

  • iOS开发-10.多线程

    1.iOS中的常见多线程方案image 2.GCD的常用函数 3.GCD的队列 4.容易混淆的术语 5.各种队列的...

  • iOS GCD 之 底层原理分析

    本文是队列创建、同步/异步函数、单例、信号量以及调度组的底层原理分析 队列创建 在上一篇文章GCD 之 函数与队列...

  • GCD的简单使用

    一. GCD基本知识 两个核心概念 队列和任务 同步函数和异步函数 二. GCD基本使用 异步函数+并发队列 : ...

  • 23.iOS底层学习之GCD函数和队列

    本章提纲:1、GCD的介绍2、函数3、队列4、队列与函数的组合使用5、GCD部分源码解读6、GCD部分习题 一、G...

网友评论

      本文标题:GCD 队列组 常用函数

      本文链接:https://www.haomeiwen.com/subject/mbsdsttx.html