objective-c - UIPrintInteractionController 仅在 iOS 14 上单击“取消”按钮时崩溃
问题描述
我的代码是:
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
NSString *url=[[req URL] absoluteString];
pi.jobName = url;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.delegate=self;
pic.showsPageRange = YES;
pic.printFormatter = self.webView.viewPrintFormatter;
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width, self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {
}];
});
它显示得很好,但是当单击取消按钮以关闭视图时,它会因消息而崩溃:
试图从除主线程或 web 线程之外的线程获取 web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃
解决方案
我认为这很容易解决,困难的部分是知道在哪里解决它。
您只提供一个代码片段,这使得它变得更加困难。这里有两个尝试。
尝试如下更改您的代码。包裹在里面的东西dispatch_async
全都变了。
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
NSString *url=[[req URL] absoluteString];
pi.jobName = url;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;
dispatch_async( dispatch_get_main_queue (), ^{
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.delegate=self;
pic.showsPageRange = YES;
pic.printFormatter = self.webView.viewPrintFormatter;
[pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width, self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {
}];
} );
我猜你是stuff
在完成处理程序中做的,所以下面是另一个确保stuff
在主线程上完成的尝试。我认为这是矫枉过正,但取决于你如何对待stuff
。
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
NSString *url=[[req URL] absoluteString];
pi.jobName = url;
pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;
dispatch_async( dispatch_get_main_queue (), ^{
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.delegate=self;
pic.showsPageRange = YES;
pic.printFormatter = self.webView.viewPrintFormatter;
[pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width, self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {
dispatch_async( dispatch_get_main_queue (), ^{
** stuff **
} );
}];
} );
推荐阅读
- ansible - 在 Ansible 中使用 ipaddr 过滤对象列表
- c# - 设置图像源导致内存泄漏(wpf,c#)
- jquery - 在循环外检索每个循环中设置的变量值
- javascript - 如何在 Node.js 模块中编写移动方法,该模块为井字游戏导出游戏类?
- batch-file - 如何将文件从子文件夹传输到两个级别的文件夹,同时将文件保留在主文件夹中并删除子文件夹?
- java - How to sum the adjacent values around a given location in a 2D Array (Matrix)?
- angular - Injection Dependency in Angular
- mysql - SQL count 函数在 equals 运算符和 in 运算符之间的区别
- android - Android emulator - error while loading state for instance 0x0 of device 'goldfish_pipe'
- reactjs - How Redirect when a Promise is Resolved?