iOS開發(fā)多線程篇—GCD的常見用法
一、延遲執(zhí)行
1.介紹 ? ?iOS常見的延時執(zhí)行有2種方式
? ? ? ? ? ? ?(1)調(diào)用NSObject的方法
? ? ? ? ? ? ? [self?performSelector:@selector(run)?withObject:nil?afterDelay:2.0];
? ? ? ? ? ? ? ? ?// 2秒后再調(diào)用self的run方法
? ? ? ? ? ? ?(2)使用GCD函數(shù)
? ? ? ? ? ? ? ?dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0?*?NSEC_PER_SEC)),?dispatch_get_main_queue(), ^{
? ? ? ? ? ? ?// 2秒后異步執(zhí)行這里的代碼...
? ? ? ? ? ? ?});
2.使用dispatch_once一次性代碼
?使用dispatch_once函數(shù)能保證某段代碼在程序運行過程中只被執(zhí)行1次
? ? ? ? ? ? static?dispatch_once_t?onceToken;
? ? ? ? ? dispatch_once(&onceToken, ^{
? ? ? ? ?//?只執(zhí)行1次的代碼(這里面默認是線程安全的)
? ? ? ?});
整個程序運行過程中,只會執(zhí)行一次。
3.使用隊列組解決 ? ? 需求:從網(wǎng)絡(luò)上下載兩張圖片,把兩張圖片合并成一張最終顯示在view上。
步驟:
創(chuàng)建一個組
開啟一個任務(wù)下載圖片1
開啟一個任務(wù)下載圖片2
同時執(zhí)行下載圖片1\下載圖片2操作
等group中的所有任務(wù)都執(zhí)行完畢, 再回到主線程執(zhí)行其他操作
//宏定義全局并發(fā)隊列
#define global_quque? ? dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
//宏定義主隊列
#define main_queue? ? ? dispatch_get_main_queue()
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (weak, nonatomic) IBOutlet UIImageView *imageView3;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//? ? 圖片1:http://d.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg
//? ? 圖片2:http://h.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg
//1.創(chuàng)建一個隊列組
dispatch_group_t group = dispatch_group_create();
//2.開啟一個任務(wù)下載圖片1
__block UIImage *image1=nil;
dispatch_group_async(group, global_quque, ^{
image1= [self imageWithUrl:@"http://d.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=2b9a12172df5e0fefa1581533d095fcd/cefc1e178a82b9019115de3d738da9773912ef00.jpg"];
NSLog(@"圖片1下載完成---%@",[NSThread currentThread]);
});
//3.開啟一個任務(wù)下載圖片2
__block UIImage *image2=nil;
dispatch_group_async(group, global_quque, ^{
image2= [self imageWithUrl:@"http://h.hiphotos.baidu.com/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=f47fd63ca41ea8d39e2f7c56f6635b2b/1e30e924b899a9018b8d3ab11f950a7b0308f5f9.jpg"];
NSLog(@"圖片2下載完成---%@",[NSThread currentThread]);
});
//同時執(zhí)行下載圖片1\下載圖片2操作
//4.等group中的所有任務(wù)都執(zhí)行完畢, 再回到主線程執(zhí)行其他操作
dispatch_group_notify(group,main_queue, ^{
NSLog(@"顯示圖片---%@",[NSThread currentThread]);
self.imageView1.image=image1;
self.imageView2.image=image2;
//合并兩張圖片
//注意最后一個參數(shù)是浮點數(shù)(0.0),不要寫成0。
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
[image1 drawInRect:CGRectMake(0, 0, 100, 100)];
[image2 drawInRect:CGRectMake(100, 0, 100, 100)];
self.imageView3.image=UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
NSLog(@"圖片合并完成---%@",[NSThread currentThread]);
});
}
-(void)download2image
{
//獲取全局并發(fā)隊列
//? ? dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//獲取主隊列
//? ? dispatch_queue_t queue= dispatch_get_main_queue();
dispatch_async(global_quque, ^{
//下載圖片1
UIImage *image1= [self imageWithUrl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
NSLog(@"圖片1下載完成---%@",[NSThread currentThread]);
//下載圖片2
UIImage *image2= [self imageWithUrl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];
NSLog(@"圖片2下載完成---%@",[NSThread currentThread]);
//回到主線程顯示圖片
dispatch_async(main_queue, ^{
NSLog(@"顯示圖片---%@",[NSThread currentThread]);
self.imageView1.image=image1;
self.imageView2.image=image2;
//合并兩張圖片
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0);
[image1 drawInRect:CGRectMake(0, 0, 100, 100)];
[image2 drawInRect:CGRectMake(0, 0, 100, 100)];
self.imageView3.image=UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文
UIGraphicsEndImageContext();
NSLog(@"圖片合并完成---%@",[NSThread currentThread]);
});
//
});
}
//封裝一個方法,傳入一個url參數(shù),返回一張網(wǎng)絡(luò)上下載的圖片
-(UIImage *)imageWithUrl:(NSString *)urlStr
{
NSURL *url=[NSURL URLWithString:urlStr];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:data];
return image;
}
@end