gpt4 book ai didi

ios - 我可以在 PDF 创建过程中更新 UI 吗?

转载 作者:行者123 更新时间:2023-11-29 13:10:33 24 4
gpt4 key购买 nike

我正在尝试为 QuickLook 框架创建多页 PDF 以进行打印,但在创建它时无法更新 UI(我希望用户获得剩余页数的倒计时)。以下代码是我尝试使用线程休眠内容代替需要一段时间才能呈现的代码的简化版本。

- (void) testPDF
{
CGRect pageSize = CGRectMake(0.0f, 0.0f, 595.28, 841.89);

UIGraphicsBeginPDFContextToFile([self filePath], pageSize, nil);
{
UIGraphicsBeginPDFPage();

for( int y = 0; y < 5; y++ ) {
self.pageLabel.text = [NSString stringWithFormat:@"%d", y];

[[UIColor grayColor] set];
[@"test string" drawAtPoint:CGPointMake(0, y*20) withFont:[UIFont systemFontOfSize:15]];

NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[NSThread sleepUntilDate:future];
}
}
UIGraphicsEndPDFContext();
}

就目前而言,我的代码呈现 PDF 但不更新 IBOutlet“pageLabel”,我们将不胜感激。

最佳答案

在后台线程上运行 PDF 创建,但在主线程(又名 UI)上更新 UI。 PDF 创建完成后,调用方法(在本例中为 presentPDF)将 PDF 呈现给用户,确保在这样做之前切换到主线程。例如:

-(void)presentPDF{

// Make sure this runs on the main (UI) thread
dispatch_sync(dispatch_get_main_queue(), ^{

//Insert code to present the PDF...

});
}

- (void) testPDF
{

// Run on background thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
CGRect pageSize = CGRectMake(0.0f, 0.0f, 595.28, 841.89);

UIGraphicsBeginPDFContextToFile([self filePath], pageSize, nil);
{
UIGraphicsBeginPDFPage();

for( int y = 0; y < 5; y++ ){

// Make sure this runs on the main (UI) thread
dispatch_sync(dispatch_get_main_queue(), ^{
self.pageLabel.text = [NSString stringWithFormat:@"%d", y];

});

[[UIColor grayColor] set];
[@"test string" drawAtPoint:CGPointMake(0, y*20) withFont:[UIFont systemFontOfSize:15]];

NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 1.0];
[NSThread sleepUntilDate:future];
}
}
UIGraphicsEndPDFContext();

[self presentPDF];
});
}

关于ios - 我可以在 PDF 创建过程中更新 UI 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17514994/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com