2012-10-02 92 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

嘿謝謝!這只是解決了我的問題。 –