關(guān)于GCD常用的方法

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、延遲執(zhí)行 1.介紹 iOS常見的延時執(zhí)行有2種方式 (1)調(diào)用NSObject的方法 [selfperform...
    在這藍色天空下閱讀 362評論 0 0
  • OK,既然講到多線程,那就一次性把全部多線程的內(nèi)容介紹完畢~ ?? 簡介 什么是GCD 全稱是Grand Cent...
    小白文_Vincent閱讀 257評論 0 1
  • 原文: 多線程編程4 - GCD 一、簡介在iOS所有實現(xiàn)多線程的方案中,GCD應(yīng)該是最有魅力的,因為GCD本身是...
    難卻卻閱讀 240評論 0 0
  • 有人說,女人嫁個好男人,就等于有了長期飯票,衣食無憂,一輩子不用愁了。 也有人說,手心向上的日子過得沒有尊嚴,沒有...
    小雅姐姐閱讀 1,262評論 0 0
  • 新開一個 Framer 特訓專欄,希望 15 期之后能對這個軟件有一個更為深入的理解。 市面上現(xiàn)在流行很多不同種類...
    求愚閱讀 844評論 0 5

友情鏈接更多精彩內(nèi)容