In iOS, the interface refresh takes place in the main thread, which causes the nsurlsession remote download picture to be set directly with Uiimageview image and not refresh the interface in a timely manner.
The following code demonstrates how to use the PerformSelectorOnMainThread:withObject:waitUntilDone: method to refresh a picture in a timely manner
1. Create an empty iOS app (application).
2. Add a controller class. Modify in yyappdelegate.m
#import "MainViewController.h" @implementation yyappdelegate-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions{ Self.window = [[UIWindow alloc] initwithframe:[[ UIScreen mainscreen] bounds]; Override point for customization after application launch. Self.window.backgroundColor = [Uicolor whitecolor]; Self.window.rootViewController = [[Mainviewcontroller alloc] Initwithnibname:nil Bundle:nil]; [Self.window makekeyandvisible]; return YES;}
3. Modify the Mainviewcontroller.m file
mainviewcontroller.m//uibycodedemo0602_imageview////Created by Yao_yu on 14-6-3.//Copyright (c) 2014 Yao_yu. All rights reserved.//#import "MainViewController.h" @interface Mainviewcontroller () @property (nonatomic, Strong) UILabel *header; @property (nonatomic, strong) Uiimageview *imageview; @property (nonatomic, strong) UIImage *imagedata;@ End@implementation mainviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super Viewdidload]; Self.imagedata = nil; Create title Tag Self.header = [[UILabel alloc] init]; Self.header.text = @ ""; Self.header.textAlignment = Nstextalignmentcenter; [Self.view AddSubview:self.header]; [Self.header Settranslatesautoresizingmaskintoconstraints:no]; Create picture View Self.imageview = [[Uiimageview alloc] init]; [Self.imagevIew setbackgroundcolor: [Uicolor Bluecolor]]; [Self.imageview setimage: [UIImage imagewithcontentsoffile:@ "/users/yao_yu/documents/aaa/3002302_.png"]; Self.imageView.layer.cornerRadius = 10; [Self.view AddSubview:self.imageView]; [Self.imageview Settranslatesautoresizingmaskintoconstraints:no]; Create a Previous button UIButton *prevbutton = [[UIButton alloc] init]; Prevbutton.frame = CGRectMake (0, 20, 300, 20); [Prevbutton Setbackgroundcolor:[uicolor Redcolor]; [Prevbutton settitle:@ "The previous one" forstate:uicontrolstatenormal]; [Prevbutton addtarget:self Action: @selector (onshowprevimage:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:prevbutton]; [Prevbutton Settranslatesautoresizingmaskintoconstraints:no]; Create the latter button UIButton *nextbutton = [[UIButton alloc] init]; Nextbutton.frame = CGRectMake (0, 20, 300, 20); [Nextbutton Setbackgroundcolor:[uicolor Redcolor]; [Nextbutton settitle:@ "After a" forstate:uicontrolstatenormal]; [Nextbutton addtarget:self Action: @selector (onshownextimage:) forcontrolevents:uicontroleventtouchupinside]; [Self.view Addsubview:nextbutton]; [Nextbutton Settranslatesautoresizingmaskintoconstraints:no]; Constraint Nsmutablearray *contraits = [Nsmutablearray array]; Nsdictionary *metrics = [nsdictionary dictionarywithobjectsandkeys:@20, @ "Vdist", @5, @ "Padding", nil]; UILabel *header = Self.header; Uiimageview *imageview = Self.imageview; Nsdictionary *views = nsdictionaryofvariablebindings (header, ImageView, Prevbutton, Nextbutton); [Contraits addobjectsfromarray:[nslayoutconstraint constraintswithvisualformat:@ "H:|-Padding-[header]-Padding-|" options:0 Metrics:metrics Views:views]]; [Contraits addobjectsfromarray:[nslayoutconstraint constraintswithvisualformat:@ "H:|-Padding-[prevButton]-(>= 0)-[nextbutton (==prevbutton)]-padding-| "options:0 metrics:metrics views:views]"; [Contraits addobjectsfromarray:[nslayoutconstraint CONSTRAINTSWITHVisualformat:@ "h:|-padding-[imageview]-padding-|" options:0 metrics:metrics views:views]; [Contraits addobjectsfromarray:[nslayoutconstraint constraintswithvisualformat:@ "V:|-VDist-[header]-Padding-[ imageview]-(>=vdist)-| "options:0 metrics:metrics views:views]"; Center vertically [Self.view addconstraint:[nslayoutconstraint Constraintwithitem:prevbutton attribute: Nslayoutattributecentery relatedby:nslayoutrelationequal ToItem:self.view Attribute:nslayoutattributecentery Multiplier:1 constant:0]]; [Self.view addconstraint:[nslayoutconstraint Constraintwithitem:nextbutton Attribute:nslayoutattributecentery Relatedby:nslayoutrelationequal ToItem:self.view attribute:nslayoutattributecentery multiplier:1 constant:0]]; [Self.view addconstraints:contraits]; }-(void) didreceivememorywarning{[super didreceivememorywarning];} -(void) Onshowprevimage: (ID) sender{Nsurl *url = [Nsurl urlwithstring:@ "http://img.gtimg.cn/images/hq_parts/hushen/ Stocks/300230.png "]; Nsurlrequest*request = [Nsurlrequest Requestwithurl:url]; Nsurlsession *session = [Nsurlsession sharedsession]; Nsurlsessiondatatask *task = [Session datataskwithrequest:request Completionhan Dler: ^ (NSData *data, Nsurlresponse *response, Nserror *error) { Self.imageView.image = nil; Self.imagedata = [UIImage imagewithdata:data]; [Self Performselectoronmainthread: @selector (updatemyimage) Withobject:nil Waituntildone:no]; }]; [Task resume]; }-(void) Onshownextimage: (ID) sender{Nsurl *url = [Nsurl urlwithstring:@ "http://img.gtimg.cn/images/hq_parts/hushen/ Stocks/300023.png "]; Nsurlrequest *request = [Nsurlrequest Requestwithurl:url]; Nsurlsession *session = [Nsurlsession sharedsession]; Nsurlsessiondatatask *task = [Session Datataskwithrequest:request Completionhandler: ^ (NSData *data, Nsurlresponse *r Esponse, Nserror *error) {self.imageView.image = nil; Self.imagedata = [UIImage imagewithdata:data]; [Self Performselectoronmainthread: @selector (updatemyimage) Withobject:nil Waituntildone:no]; }]; [Task resume];} -(void) updatemyimage{if (!self.imageview.image) self.imageView.image = Self.imagedata; return;} @end4. Running