1

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

-(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

4

2 回答 2

4

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

于 2012-10-02T06:40:14.817 回答
4

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

dispatch_async(dispatch_get_main_queue(), ^{
    [alertview show];
});
于 2014-07-16T17:39:51.143 回答