IOS multithreading-loading an image using a thread-NSThread (1)

Source: Internet
Author: User

Ui update operations in IOS are actually the same as those in Android. They are not time-consuming operations in the main thread. Therefore, you need to enable new threads to perform these operations, to avoid blocking the main thread. After the operations in the new thread are completed, the main thread is called to update the UI. The following is an example:

1. Declare the imageview Control

1 #import <UIKit/UIKit.h>2 3 @interface DemoDispatchQueueViewController : UIViewController4 @property(nonatomic,strong) UIImageView *imageView;5 @end

2. Complete Functions

1 # import "demodispatchqueueviewcontroller. H "2 3 @ interface demodispatchqueueviewcontroller () 4 5 @ end 6 7 @ implementation limit 8 @ synthesize imageview; 9-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) nibbundleornil10 {11 self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; 12 if (Self) {13} 14 return self; 15} 16 17-(void) viewdidload18 {19 [Super viewdidload]; 20 // do any additional setup after loading the view.21 self. title = @ "GCD Demo"; 22 23 self. imageview = [[uiimageview alloc] initwithframe: cgrectmake (10, 10,300,380)]; 24 25 self. imageview. contentmode = uiviewcontentmodescaletofill; 26 27 [self. view addsubview: imageview]; 28 29[NSThread detachNewThreadSelector: @ selector (loadImageByUrl :) toTarget: self withObject: @ "http://image.rayliimg.cn/0008/2009-01-15/images/2009115135825184.jpg"];30 31} 32 33-(void) loadImageByUrl :( NSString *) imageUrl34 {35 NSLog (@ "url is: % @", imageUrl); 36 NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: imageUrl]; 37 UIImage * image = [UIImage imageWithData: data]; 38 39 if (image! = Nil) {40[Self defined mselecw.mainthread: @ selector (setImage :) withObject: image waitUntilDone: YES];41} else {42 NSLog (@ "the corresponding image cannot be loaded"); 43} 44} 45-(void) setImage :( UIImage *) image {46 [self. imageView setImage: image]; 47} 48 49 50-(void) didReceiveMemoryWarning51 {52 [super didReceiveMemoryWarning]; 53 // Dispose of any resources that can be recreated.54} 55 56 @ end

 

 

Related Article

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.