iOS multithreaded GCD Group

Source: Internet
Author: User

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 @end8 9 @implementation HmviewcontrollerTen  One- (void) Viewdidload A { - [Super Viewdidload]; -     //additional setup after loading the view, typically from a nib. the      -     //Picture 1:http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg -     //Picture 2:http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg -     /** + 1. Download picture 1 and picture 2 -       + 2. Merge picture 1 and picture 2 into one picture and display to ImageView A       at thinking: - * Download Image: Sub-thread - * Wait for 2 images to be downloaded before returning to the main thread -      */ - } -  in- (void) Touchesbegan: (Nsset *) touches withevent: (Uievent *)Event - { to     //Creating a group +dispatch_group_t Group =dispatch_group_create (); -      the     //open a task download image 1 *__block UIImage *image1 =Nil; $Dispatch_group_async (Group, global_queue, ^{Panax NotoginsengImage1 = [Self Imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"]; -     }); the      +     //open a task download Image 2 A__block UIImage *image2 =Nil; theDispatch_group_async (Group, global_queue, ^{ +Image2 = [Self Imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/b2a9cfc88b7a56cfa59b8d09208fa1fb.jpg"]; -     }); $      $     //simultaneous download image 1\ download Image 2 Operation -      -     //All tasks in the group are completed, and then go back to the main thread to perform other operations theDispatch_group_notify (Group, main_queue, ^{ -Self.imageView1.image =Image1;WuyiSelf.imageView2.image =Image2; the          -         //Merging WuUigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, -), NO,0.0); -[Image1 Drawinrect:cgrectmake (0,0, -, -)]; About[Image2 Drawinrect:cgrectmake ( -,0, -, -)]; $Self.bigImageView.image =Uigraphicsgetimagefromcurrentimagecontext (); -         //Close Context - Uigraphicsendimagecontext (); -     }); A //if (Self.log = = NO) { + //NSLog (@ "-------Touchesbegan"); the //self.log = YES; - //    } $ //static dispatch_once_t Oncetoken; the //dispatch_once (&oncetoken, ^{ the //NSLog (@ "-------Touchesbegan"); the //    }); the } -  in- (void) Downlaod2image the { theDispatch_async (Global_queue, ^{ AboutNSLog (@"Download Image---%@", [Nsthread CurrentThread]); the          the         //Download Image 1 theUIImage *image1 = [Self Imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"]; +NSLog (@"Download Picture 1---%@", [Nsthread CurrentThread]); -         //Download Image 2 theUIImage *image2 = [Self Imagewithurl:@"http://news.baidu.com/z/resource/r/image/2014-06-22/2a1009253cf9fc7c97893a4f0fe3a7b1.jpg"];BayiNSLog (@"Download Picture 2---%@", [Nsthread CurrentThread]); the          theDispatch_async (Main_queue, ^{ -NSLog (@"Show Pictures---%@", [Nsthread CurrentThread]); -              theSelf.imageView1.image =Image1; theSelf.imageView2.image =Image2; the              the             //Merging -Uigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, -), NO,0.0); the[Image1 Drawinrect:cgrectmake (0,0, -, -)]; the[Image2 Drawinrect:cgrectmake ( -,0, -, -)]; theSelf.bigImageView.image =Uigraphicsgetimagefromcurrentimagecontext ();94             //Close Context the Uigraphicsendimagecontext (); the         }); the     });98 } About  --(UIImage *) Imagewithurl: (NSString *) Urlstr101 {102Nsurl *url =[Nsurl Urlwithstring:urlstr];103NSData *data = [NSData Datawithcontentsofurl:url];//This guild is time consuming .104     return[UIImage Imagewithdata:data]; the }106 107- (void) Delay108 {109     //NSLog (@ "----Touchesbegan----%@", [Nsthread CurrentThread]); the     111     //Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{ the     //[Self performselector: @selector (Run) Withobject:nil afterdelay:2.0];113     //    }); the     //1. Global Concurrency Queue thedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); the     117     //2. Calculate time for task execution118dispatch_time_t when = Dispatch_time (Dispatch_time_now, (int64_t) (5.0*nsec_per_sec));119      -     //3. The task in the queue will be executed at this point in time121Dispatch_after (when, queue, ^{122NSLog (@"----Run----%@", [Nsthread CurrentThread]);123     });124 } the 126@end

iOS multithreaded GCD Group

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.