队列组
@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