UIAlertView中抽出时间来证明(UIAlertView taking time to sho

2019-10-17 06:32发布

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

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

Answer 1:

警报只会在当前运行循环的末尾显示出来,所以你所做的一切之后“显示”,会实际发生之前。 请注意,这将是一个好主意,该代码移动到背景,无论如何,因为它会以其他方式阻止主线程,使应用程序无响应的最好的。



Answer 2:

移动[alertview show]; 到主队列。 如果你打算从显示异步回调的警告,这是很有帮助。

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


文章来源: UIAlertView taking time to show