UIAlertView需要时间来显示

我想显示按下button的警报视图。 显示它,但在屏幕上显示需要太多时间。 这里是我按下button的function:

-(void)saveAndClose:(id)sender { waitForOCR=[[UIAlertView alloc] initWithTitle:@"Processing..." message:@"Please wait." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil]; waitForOCR.delegate=self; [waitForOCR show]; if(!isCancelled){ NSMutableArray *dateSaved=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"dateCaptured"]]; NSDateFormatter *dateText=[[NSDateFormatter alloc] init]; [dateText setDateFormat:@"yyyy-MM-dd"]; NSDate *now=[NSDate date]; NSString *dateValue=[dateText stringFromDate:now]; [dateSaved addObject:dateValue]; NSMutableArray *timeSaved=[[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] arrayForKey:@"timeCaptured"]]; NSDateFormatter *timeText=[[NSDateFormatter alloc] init]; [timeText setDateFormat:@"HH:mm:ss"]; NSString *timeValue=[timeText stringFromDate:now]; [timeSaved addObject:timeValue]; [[NSUserDefaults standardUserDefaults] setObject:dateSaved forKey:@"dateCaptured"]; [[NSUserDefaults standardUserDefaults] setObject:timeSaved forKey:@"timeCaptured"]; [dateSaved release]; dateSaved=nil; [timeSaved release]; timeSaved=nil; int counter = [[NSUserDefaults standardUserDefaults] integerForKey:@"LaunchesCounter"]; counter++; [[NSUserDefaults standardUserDefaults] setInteger:counter forKey:@"LaunchesCounter"]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%d.png",counter]]; NSData *thedata = [[NSData alloc] initWithData:UIImagePNGRepresentation(imageToCrop)]; [thedata writeToFile:localFilePath atomically:YES]; [thedata release]; [NSThread detachNewThreadSelector:@selector(processOcrAt:) toTarget:self withObject:[self.imageCropper getCroppedImage]]; } else{ isCancelled=FALSE; } } 

正如你所看到的,我在函数的开头显示UIAlerView 。 但显示时间太长。 任何build议来解决这个问题? 感谢问候Idrees

警报只会显示在当前运行循环结束时,所以您在“显示”之后所做的所有事情实际上都会发生。 请注意,将代码移动到后台将是一个好主意,因为否则它将阻止主线程,并使应用程序无法响应。

移动[alertview show]; 到主队列。 如果您打算显示来自asynchronouscallback的警报,这很有帮助。

 dispatch_async(dispatch_get_main_queue(), ^{ [alertview show]; });