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