2012-10-02 56 views
1

我想在按下按钮时显示警报视图。显示它,但在屏幕上显示需要很长时间。这里要说的是,我在按下按钮调用该函数:UIAlertView需要时间显示

-(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。但显示时间过长。 任何建议来解决这个问题? 感谢 问候 Idrees

+1

当然,因为你正在写在主线程'[海图将writeToFile:localFilePath原子:YES]到文件;' – Nekto

+0

但写入文件之前,我显示UIAlertView中。它显示,但过了一段时间。 –

回答

4

警报只会在当前运行循环的末尾显示出来,所以你所做的一切之后“显示”,会实际发生之前。请注意,将代码移到背景中会是一个好主意,因为否则它会阻止主线程并使应用程序无法响应。

+0

100%正确答案:)非常感谢你 –

4

[alertview show];移动到主队列中。如果您打算显示来自异步回调的警报,这很有用。

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

嘿谢谢!这只是解决了我的问题。 –