Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Merge picture (picture watermark) The first method effect
Realize:
Ideas:
1. Download 2 pictures respectively: Big picture, LOGO
2. Merge 2 photos
3. Display to a ImageView body
//Asynchronous Download Dispatch_async(Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{//1. Download the 1th one Nsurl*URL1 = [Nsurlurlwithstring:@"Http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; NSData *data1 = [NSData DATAWITHCONTENTSOFURL:URL1];UIImage*image1 = [UIImageIMAGEWITHDATA:DATA1];//2. Download the 2nd one Nsurl*URL2 = [Nsurlurlwithstring:@"Http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; NSData *data2 = [NSData datawithcontentsofurl:url2];UIImage*image2 = [UIImageIMAGEWITHDATA:DATA2];//3. Merging pictures //Open a bitmap contextUigraphicsbeginimagecontextwithoptions (Image1. Size,NO,0.0);//Draw 1th picture CGFloatIMAGE1W = Image1. Size. Width;CGFloatimage1h = Image1. Size. Height; [Image1 Drawinrect:cgrectmake (0,0, IMAGE1W, image1h)];//Draw 2nd picture CGFloatimage2w = Image2. Size. Width*0.5;CGFloatIMAGE2H = Image2. Size. Height*0.5;CGFloatimage2y = image1h-image2h; [Image2 Drawinrect:cgrectmake (0, image2y, image2w, image2h)];//Get the picture in context UIImage*fullimage = Uigraphicsgetimagefromcurrentimagecontext ();//End ContextUigraphicsendimagecontext ();//4. Back to the main thread display picture Dispatch_async(Dispatch_get_main_queue (), ^{ Self. ImageView. Image= Fullimage; }); });
But this is done in an asynchronous thread, but when the first diagram is downloaded, the second graph is still waiting, and we want each diagram to be downloaded in a separate asynchronous thread, so we use the second method.
: The second method
First define two ImageView
@property (weaknonatomicIBOutletUIImageView *imageView;@property (nonatomicstrongUIImage *image1;@property (nonatomicstrongUIImage *image2;
And then implement this implementation
//Asynchronous Download Dispatch_async(Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{//1. Download the 1th one Nsurl*URL1 = [Nsurlurlwithstring:@"Http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; NSData *data1 = [NSData DATAWITHCONTENTSOFURL:URL1]; Self. Image1= [UIImageIMAGEWITHDATA:DATA1]; [ SelfBindimages]; });Dispatch_async(Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{//2. Download the 2nd one Nsurl*URL2 = [Nsurlurlwithstring:@"Http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; NSData *data2 = [NSData datawithcontentsofurl:url2]; Self. Image2= [UIImageIMAGEWITHDATA:DATA2]; [ SelfBindimages]; });
It works by opening two asynchronous threads to download two images, which will be called after the download is complete.
"Self bindimages" method, this time we set in the S method back to the main interface refresh, but first to determine whether the two graphs have data, as follows:
- (void) bindimages{if( Self. Image1==Nil|| Self. Image2==Nil)return;//3. Merging pictures //Open a bitmap contextUigraphicsbeginimagecontextwithoptions ( Self. Image1. Size,NO,0.0);//Draw 1th picture CGFloatIMAGE1W = Self. Image1. Size. Width;CGFloatimage1h = Self. Image1. Size. Height; [ Self. Image1Drawinrect:cgrectmake (0,0, IMAGE1W, image1h)];//Draw 2nd picture CGFloatimage2w = Self. Image2. Size. Width*0.5;CGFloatIMAGE2H = Self. Image2. Size. Height*0.5;CGFloatimage2y = image1h-image2h; [ Self. Image2Drawinrect:cgrectmake (0, image2y, image2w, image2h)];//Get the picture in context UIImage*fullimage = Uigraphicsgetimagefromcurrentimagecontext ();//End ContextUigraphicsendimagecontext ();//4. Back to the main thread display picture Dispatch_async(Dispatch_get_main_queue (), ^{ Self. ImageView. Image= Fullimage; });}
Although we solve the problem through our own ideas, we have a better solution, that is, the queue group
Third method: When does queue group complete use queue group?
First: Perform 2 time-consuming operations asynchronously, respectively
Second: Wait for 2 asynchronous operations to complete, then return to the main thread to perform the operation
If you want to implement these requirements quickly and efficiently, consider using queue groups, as follows:
// 1.创建队列组group = dispatch_group_create();// 2.1第一个队列组异步dispatch_group_async(group0), ^{ // 执行1个耗时的异步操作});// 2.2第二个队列组异步dispatch_group_async(group0), ^{ // 执行1个耗时的异步操作});// 3.所有队列组异步线程结束后dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 等前面的异步操作都执行完毕后,回到主线程...});
Then let's do it with the queue group, the code is as follows:
//1. Queue Groupdispatch_group_t group = Dispatch_group_create ();dispatch_queue_tQueue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);//2. Download Picture 1__blockUIImage*image1 =Nil; Dispatch_group_async (group, queue, ^{Nsurl*URL1 = [Nsurlurlwithstring:@"Http://g.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494ee460de6182ff5e0fe99257e80.jpg"]; NSData *data1 = [NSData DATAWITHCONTENTSOFURL:URL1]; Image1 = [UIImageIMAGEWITHDATA:DATA1]; });//3. Download Picture 2__blockUIImage*image2 =Nil; Dispatch_group_async (group, queue, ^{Nsurl*URL2 = [Nsurlurlwithstring:@"Http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png"]; NSData *data2 = [NSData datawithcontentsofurl:url2]; Image2 = [UIImageIMAGEWITHDATA:DATA2]; });//4. Merge pictures (ensure that after all tasks in the group are executed, the block inside the Notify function is executed)Dispatch_group_notify (group, queue, ^{//Open a bitmap contextUigraphicsbeginimagecontextwithoptions (Image1. Size,NO,0.0);//Draw 1th picture CGFloatIMAGE1W = Image1. Size. Width;CGFloatimage1h = Image1. Size. Height; [Image1 Drawinrect:cgrectmake (0,0, IMAGE1W, image1h)];//Draw 2nd picture CGFloatimage2w = Image2. Size. Width*0.5;CGFloatIMAGE2H = Image2. Size. Height*0.5;CGFloatimage2y = image1h-image2h; [Image2 Drawinrect:cgrectmake (0, image2y, image2w, image2h)];//Get the picture in context UIImage*fullimage = Uigraphicsgetimagefromcurrentimagecontext ();//End ContextUigraphicsendimagecontext ();//5. Back to the main thread display picture Dispatch_async(Dispatch_get_main_queue (), ^{ Self. ImageView. Image= Fullimage; }); });
__block keywords
Note: Here we use a __block keyword, because if not added, the block is not allowed to access the outside of the block of things.
Small Summary: Open asynchronous
(Direct knock Dispatch_a ... Can come out a bunch of hints, and then choose to fill, no difficulty, said before.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ code});
To open a queue group:
First build a queue group:
dispatch_group_t group = dispatch_group_create();
Then change the queue to a queue and use Dispatch_get_global_queue.
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // 执行1个耗时的异步操作});
You can get a few more on the top, and a couple of threads.
And then at the end, use this:
dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 等前面的异步操作都执行完毕后,回到主线程...});
The process of merging pictures
Steps:
1. Open a Bitmap context
2.1 Drawing the 1th picture
2.2 Drawing the 2nd picture
3. Get the picture in context
4. End Context
//1. Open a bitmap contextUigraphicsbeginimagecontextwithoptions (Image1. Size,NO,0.0);///2.1 Draw 1th Picture CGFloatIMAGE1W = Image1. Size. Width;CGFloatimage1h = Image1. Size. Height; [Image1 Drawinrect:cgrectmake (0,0, IMAGE1W, image1h)];///2.2 Draw 2nd picture CGFloatimage2w = Image2. Size. Width*0.5;CGFloatIMAGE2H = Image2. Size. Height*0.5;CGFloatimage2y = image1h-image2h; [Image2 Drawinrect:cgrectmake (0, image2y, image2w, image2h)];//3. Get the picture in context UIImage*fullimage = Uigraphicsgetimagefromcurrentimagecontext ();//4. End ContextUigraphicsendimagecontext ();
Cat learn iOS (51) Multi-threaded network GCD Download merged pictures _ use of queue groups