標籤:
1 #import "HMViewController.h" 2 3 @interface HMViewController () 4 @property (weak, nonatomic) IBOutlet UIImageView *imageView1; 5 @property (weak, nonatomic) IBOutlet UIImageView *imageView2; 6 @property (weak, nonatomic) IBOutlet UIImageView *bigImageView; 7 @end 8 9 @implementation HMViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 16 // 圖片1: http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg 17 // 圖片2: http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg 18 /** 19 1.下載圖片1和圖片2 20 21 2.將圖片1和圖片2合并成一張圖片後顯示到imageView上 22 23 思考: 24 * 下載圖片 : 子線程 25 * 等2張圖片都下載完畢後, 才回到主線程 26 */ 27 } 28 29 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 30 { 31 // 建立一個組 32 dispatch_group_t group = dispatch_group_create(); 33 34 // 開啟一個任務下載圖片1 35 __block UIImage *image1 = nil; 36 dispatch_group_async(group, global_queue, ^{ 37 image1 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"]; 38 }); 39 40 // 開啟一個任務下載圖片2 41 __block UIImage *image2 = nil; 42 dispatch_group_async(group, global_queue, ^{ 43 image2 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg"]; 44 }); 45 46 // 同時執行下載圖片1\下載圖片2操作 47 48 // 等group中的所有任務都執行完畢, 再回到主線程執行其他動作 49 dispatch_group_notify(group, main_queue, ^{ 50 self.imageView1.image = image1; 51 self.imageView2.image = image2; 52 53 // 合并 54 UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0); 55 [image1 drawInRect:CGRectMake(0, 0, 100, 100)]; 56 [image2 drawInRect:CGRectMake(100, 0, 100, 100)]; 57 self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 58 // 關閉上下文 59 UIGraphicsEndImageContext(); 60 }); 61 // if (self.log == NO) { 62 // NSLog(@"-------touchesBegan"); 63 // self.log = YES; 64 // } 65 // static dispatch_once_t onceToken; 66 // dispatch_once(&onceToken, ^{ 67 // NSLog(@"-------touchesBegan"); 68 // }); 69 } 70 71 - (void)downlaod2image 72 { 73 dispatch_async(global_queue, ^{ 74 NSLog(@"下載圖片---%@", [NSThread currentThread]); 75 76 // 下載圖片1 77 UIImage *image1 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"]; 78 NSLog(@"下載完圖片1---%@", [NSThread currentThread]); 79 // 下載圖片2 80 UIImage *image2 = [self imageWithURL:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"]; 81 NSLog(@"下載完圖片2---%@", [NSThread currentThread]); 82 83 dispatch_async(main_queue, ^{ 84 NSLog(@"顯示圖片---%@", [NSThread currentThread]); 85 86 self.imageView1.image = image1; 87 self.imageView2.image = image2; 88 89 // 合并 90 UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 100), NO, 0.0); 91 [image1 drawInRect:CGRectMake(0, 0, 100, 100)]; 92 [image2 drawInRect:CGRectMake(100, 0, 100, 100)]; 93 self.bigImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 94 // 關閉上下文 95 UIGraphicsEndImageContext(); 96 }); 97 }); 98 } 99 100 - (UIImage *)imageWithURL:(NSString *)urlStr101 {102 NSURL *url = [NSURL URLWithString:urlStr];103 NSData *data = [NSData dataWithContentsOfURL:url]; // 這行會比較耗時104 return [UIImage imageWithData:data];105 }106 107 - (void)delay108 {109 // NSLog(@"----touchesBegan----%@", [NSThread currentThread]);110 111 // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{112 // [self performSelector:@selector(run) withObject:nil afterDelay:2.0];113 // });114 // 1.全域並發隊列115 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);116 117 // 2.計算任務執行的時間118 dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC));119 120 // 3.會在when這個時間點, 執行queue中的任務121 dispatch_after(when, queue, ^{122 NSLog(@"----run----%@", [NSThread currentThread]);123 });124 }125 126 @end
ios多線程GCD group