列出了隊列組的用法以及和隊列組同樣效果的實現方式;共三種實現方式。 ...
1 #import "XZViewController.h" 2 3 // 1.分別下載2張圖片:大圖片、LOGO 4 // 2.合併2張圖片 5 // 3.顯示到一個imageView身上 6 7 @interface XZViewController () 8 @property (weak, nonatomic) IBOutlet UIImageView *imageView; 9 @property (nonatomic, strong) UIImage *image1; 10 @property (nonatomic, strong) UIImage *image2; 11 @end 12 13 @implementation XZViewController 14 15 - (void)viewDidLoad 16 { 17 [super viewDidLoad]; 18 19 } 20 21 // 2D繪圖 Quartz2D 22 // 合併圖片 -- 水印 23 24 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 25 { // 方法三 26 // 1.隊列組 27 dispatch_group_t group = dispatch_group_create(); 28 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 29 30 // 2.下載圖片1 31 __block UIImage *image1 = nil; 32 dispatch_group_async(group, queue, ^{ 33 NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; 34 NSData *data1 = [NSData dataWithContentsOfURL:url1]; 35 image1 = [UIImage imageWithData:data1]; 36 }); 37 38 // 3.下載圖片2 39 __block UIImage *image2 = nil; 40 dispatch_group_async(group, queue, ^{ 41 NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; 42 NSData *data2 = [NSData dataWithContentsOfURL:url2]; 43 image2 = [UIImage imageWithData:data2]; 44 }); 45 46 // 4.合併圖片 (保證執行完組裡面的所有任務之後,再執行notify函數裡面的block) 47 dispatch_group_notify(group, queue, ^{ 48 // 開啟一個點陣圖上下文 49 UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0); 50 51 // 繪製第1張圖片 52 CGFloat image1W = image1.size.width; 53 CGFloat image1H = image1.size.height; 54 [image1 drawInRect:CGRectMake(0, 0, image1W, image1H)]; 55 56 // 繪製第2張圖片 57 CGFloat image2W = image2.size.width * 0.5; 58 CGFloat image2H = image2.size.height * 0.5; 59 CGFloat image2Y = image1H - image2H; 60 [image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)]; 61 62 // 得到上下文中的圖片 63 UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); 64 65 // 結束上下文 66 UIGraphicsEndImageContext(); 67 68 // 5.回到主線程顯示圖片 69 dispatch_async(dispatch_get_main_queue(), ^{ 70 self.imageView.image = fullImage; 71 }); 72 }); 73 } 74 /** 方法二 */ 75 - (void)test2 76 { 77 // 非同步下載 78 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 79 // 1.下載第1張 80 NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; 81 NSData *data1 = [NSData dataWithContentsOfURL:url1]; 82 self.image1 = [UIImage imageWithData:data1]; 83 84 [self bindImages]; 85 }); 86 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 87 // 2.下載第2張 88 NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; 89 NSData *data2 = [NSData dataWithContentsOfURL:url2]; 90 self.image2 = [UIImage imageWithData:data2]; 91 92 [self bindImages]; 93 }); 94 } 95 96 - (void)bindImages 97 { 98 if (self.image1 == nil || self.image2 == nil) return; 99 100 // 3.合併圖片 101 // 開啟一個點陣圖上下文 102 UIGraphicsBeginImageContextWithOptions(self.image1.size, NO, 0.0); 103 104 // 繪製第1張圖片 105 CGFloat image1W = self.image1.size.width; 106 CGFloat image1H = self.image1.size.height; 107 [self.image1 drawInRect:CGRectMake(0, 0, image1W, image1H)]; 108 109 // 繪製第2張圖片 110 CGFloat image2W = self.image2.size.width * 0.5; 111 CGFloat image2H = self.image2.size.height * 0.5; 112 CGFloat image2Y = image1H - image2H; 113 [self.image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)]; 114 115 // 得到上下文中的圖片 116 UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); 117 118 // 結束上下文 119 UIGraphicsEndImageContext(); 120 121 // 4.回到主線程顯示圖片 122 dispatch_async(dispatch_get_main_queue(), ^{ 123 self.imageView.image = fullImage; 124 }); 125 } 126 /** 方法一 */ 127 - (void)test1 128 { 129 // 非同步下載 130 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 131 // 1.下載第1張 132 NSURL *url1 = [NSURL URLWithString:@"http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; 133 NSData *data1 = [NSData dataWithContentsOfURL:url1]; 134 UIImage *image1 = [UIImage imageWithData:data1]; 135 136 // 2.下載第2張 137 NSURL *url2 = [NSURL URLWithString:@"http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; 138 NSData *data2 = [NSData dataWithContentsOfURL:url2]; 139 UIImage *image2 = [UIImage imageWithData:data2]; 140 141 // 3.合併圖片 142 // 開啟一個點陣圖上下文 143 UIGraphicsBeginImageContextWithOptions(image1.size, NO, 0.0); 144 145 // 繪製第1張圖片 146 CGFloat image1W = image1.size.width; 147 CGFloat image1H = image1.size.height; 148 [image1 drawInRect:CGRectMake(0, 0, image1W, image1H)]; 149 150 // 繪製第2張圖片 151 CGFloat image2W = image2.size.width * 0.5; 152 CGFloat image2H = image2.size.height * 0.5; 153 CGFloat image2Y = image1H - image2H; 154 [image2 drawInRect:CGRectMake(0, image2Y, image2W, image2H)]; 155 156 // 得到上下文中的圖片 157 UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext(); 158 159 // 結束上下文 160 UIGraphicsEndImageContext(); 161 162 // 4.回到主線程顯示圖片 163 dispatch_async(dispatch_get_main_queue(), ^{ 164 self.imageView.image = fullImage; 165 }); 166 }); 167 } 168 169 @end