03-queue group (understanding), 03-queue group understanding
1 # import "XZViewController. h "2 3 // 1. download two images: large image, LOGO 4 // 2. merge two images. 5 // 3. 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 // merge the image -- watermark 23 24-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event 25 {// method 3 26 // 1. queue group 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. download Picture 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. download picture 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. merge the image (ensure that all the tasks in the group are executed, and then execute the block in the notify function) 47 dispatch_group_notify (group, queue, ^ {48 // enable a bitmap context 49 uigraphicsbeginimagecontextwitexceptions (image1.size, NO, 0.0); 50 51 // draw 1st images 52 CGFloat image1W = image1.size. width; 53 CGFloat image1H = image1.size. height; 54 [image1 drawInRect: CGRectMake (0, 0, image1W, image1H)]; 55 56 // draw 2nd images 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 // get the image 63 UIImage * fullImage = UIGraphicsGetImageFromCurrentImageContext (); 64 65 // end context 66 UIGraphicsEndImageContext (); 67 68/5. return to the main thread to display the image 69 dispatch_async (dispatch_get_main_queue (), ^ {70 self. imageView. image = fullImage; 71}); 72}); 73} 74/** method 2 */75-(void) test2 76 {77 // asynchronous download 78 dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {79 // 1. download 1st sheets of 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 (queue, 0), ^ {87/2. download 2nd sheets of 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. merge Image 101 // enable a bitmap context 102 uigraphicsbeginimagecontextwitexceptions (self. image1.size, NO, 0.0); 103 104 // draw 1st images 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 // draw 2nd images 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 // obtain the image 116 UIImage * fullImage = UIGraphicsGetImageFromCurrentImageContext () in the context (); 117 118 // end context 119 UIGraphicsEndImageContext (); 120 121 // 4. return to the main thread to display the image 122 dispatch_async (dispatch_get_main_queue (), ^ {123 self. imageView. image = fullImage; 124}); 125} 126/** method 1 */127-(void) test1128 {129 // asynchronously download 130 dispatch_async (dispatch_get_global_queue (queue, 0 ), ^ {131 // 1. download 1st NSURL * url1 = [NSURL URLWithString: @ "http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; 132 NSData * data1 = [NSData dataWithContentsOfURL: url1]; 133 UIImage * image1 = [UIImage imageWithData: data1]; 135 136 // 2. download 2nd NSURL * url2 = [NSURL URLWithString: @ "http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; 137 NSData * data2 = [NSData dataWithContentsOfURL: url2]; 138 UIImage * image2 = [UIImage imageWithData: data2]; 140 141 // 3. merge Image 142 // enable a bitmap context 143 uigraphicsbeginimagecontextwitexceptions (image1.size, NO, 0.0); 144 145 1st // draw 146 images CGFloat image1W = image1.size. width; 147 CGFloat image1H = image1.size. height; 148 [image1 drawInRect: CGRectMake (0, 0, image1W, image1H)]; 149 150 2nd // draw 151 images 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 // obtain the image 157 UIImage * fullImage = UIGraphicsGetImageFromCurrentImageContext (); 158 159 160 // end context 161 UIGraphicsEndImageContext (); 162 // 4. return to the main thread to display the image 163 dispatch_async (dispatch_get_main_queue (), ^ {164 self. imageView. image = fullImage; 165}); 166}); 167} 168 169 @ end