Swift 正在停止NSPrintOperation以防止应用程序崩溃
我有以下几行代码来打印webView内容 问题是,当用户与打印操作面板交互时,它有时会导致应用程序崩溃 如果我只单击cancel按钮,应用程序可能会崩溃。如果我尝试设置一个不同的比例,它有时会崩溃。我不知道为什么应用程序会崩溃。这总是最后一行。我做错了什么?谢谢Swift 正在停止NSPrintOperation以防止应用程序崩溃,swift,cocoa,swift3,nsprintoperation,Swift,Cocoa,Swift3,Nsprintoperation,我有以下几行代码来打印webView内容 问题是,当用户与打印操作面板交互时,它有时会导致应用程序崩溃 如果我只单击cancel按钮,应用程序可能会崩溃。如果我尝试设置一个不同的比例,它有时会崩溃。我不知道为什么应用程序会崩溃。这总是最后一行。我做错了什么?谢谢 当选项中没有.showsCopies时,打印面板代码中存在错误。它有一个使用后免费的错误和副本相关的子视图。它将它们从视图层次结构中删除,从而允许取消分配它们。但是,它保持对文本字段的悬空引用,并尝试在各种情况下对其进行操作。例如,它有
当选项中没有.showsCopies时,打印面板代码中存在错误。它有一个使用后免费的错误和副本相关的子视图。它将它们从视图层次结构中删除,从而允许取消分配它们。但是,它保持对文本字段的悬空引用,并尝试在各种情况下对其进行操作。例如,它有时试图让自己成为第一反应者
基本上,您不应该试图抑制“副本”字段。从OS X URL打印pdf的我的代码:
let printInfo = NSPrintInfo.shared
let manager = FileManager.default
do{
let directoryURL = try manager.url(for: .documentDirectory, in:.userDomainMask, appropriateFor:nil, create:true)
let docURL = NSURL(string:"XX.pdf", relativeTo:directoryURL)
let pdfDoc = PDFDocument.init(url: docURL! as URL)
let page = CGRect(x: 0, y: 0, width: 595.2, height: 1841.8) // A4, 72 dpi
let pdfView : PDFView = PDFView.init(frame: page)
pdfView.document = pdfDoc
let operation: NSPrintOperation = NSPrintOperation(view: pdfView, printInfo: printInfo)
operation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
operation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
operation.run()
}catch{
}
谢谢你的意思是,我只需要在选项字典中包含showsCopies?如果我问G人关于“showsCopies NSPrintOperation”的问题,我没有发现任何文献。应用程序仍然偶尔会出现“showsCopies”崩溃。对不起,我现在已经修复了一个拼写错误。是的,有三个ss的showsCopies。可能还有其他类似的bug。试着不要设置选项,这样你就可以使用默认设置,而苹果大概已经对这些默认设置进行了更彻底的测试。如果确实如此,请尝试,而不是从头开始构建选项集,检索默认选项并仅添加.showscaling。
let printInfo = NSPrintInfo.shared
let manager = FileManager.default
do{
let directoryURL = try manager.url(for: .documentDirectory, in:.userDomainMask, appropriateFor:nil, create:true)
let docURL = NSURL(string:"XX.pdf", relativeTo:directoryURL)
let pdfDoc = PDFDocument.init(url: docURL! as URL)
let page = CGRect(x: 0, y: 0, width: 595.2, height: 1841.8) // A4, 72 dpi
let pdfView : PDFView = PDFView.init(frame: page)
pdfView.document = pdfDoc
let operation: NSPrintOperation = NSPrintOperation(view: pdfView, printInfo: printInfo)
operation.printPanel.options.insert(NSPrintPanel.Options.showsPaperSize)
operation.printPanel.options.insert(NSPrintPanel.Options.showsOrientation)
operation.run()
}catch{
}