/**
- 线程组
*/
-(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:@""];
//加载图片
NSData *data = [NSData dataWithContentsOfURL:url];
//生成图片
self.image1 = [UIImage imageWithData:data];
});
//2、下载图片2
dispatch_group_async(group, queue, ^{
//图片路径
NSURL *url = [NSURL URLWithString:@""];
//加载图片
NSData *data = [NSData dataWithContentsOfURL:url];
//生成图片
self.image2 = [UIImage imageWithData:data];
});
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(), ^{
self.imageView.image = image;
});
});
}
/**
- GCD的文件剪切
*/
-(void)GCDMoveFile
{
NSString *from = @"";
NSString *to = @"";
NSFileManager *mgr = [NSFileManager defaultManager];
NSArray *subPaths = [mgr subpathsAtPath:from];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
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];
});
}
/**
- 传统的文件剪切
*/
-(void)moveFile
{
NSString *from = @"";
NSString *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];
//剪切
[mgr moveItemAtPath:fromFullPath toPath:toFullPath error:nil];
}
}
-(void)GCD
{
//全局
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//回到主线,刷新UI
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
});
});
});
});
#pragma mark 一次性代码
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@" ---------run");
//整个程序只执行一次(线程是安全的)
});
```
//快速遍历,快速迭代
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_apply(10, queue, ^(size_t index) {
NSLog(@"%zd --- %@",index,[NSThread currentThread].name);
});
pragma mark ---- 栅栏函数
//创建一个并发队列
// label "相当于队列的名字"
dispatch_queue_t queue1 = dispatch_queue_create("NEME.D", DISPATCH_QUEUE_CONCURRENT);
dispatch_barrier_async(queue1, ^{
//在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后 才会执行
// 这个queue不能是全局的并发队列;
});
pragma mark -- 延迟执行 --
// GCD延迟函数
dispatch_after(dispatch_time( DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
});
//调用NSObject的方法
[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
// 使用NSTmer
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(run) userInfo:nil repeats:NO];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//开启子线程
[self performSelectorInBackground:@selector(downLoad) withObject:nil];
}
-(void)downLoad
{
NSURL *url = [NSURL URLWithString:@""];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//回到主线程 ,展示图片
[self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
//方法2
// [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];
}