3

这个问题刚刚出现在 iOS 13 中,之前不是问题。如果我向UIPrintInteractionController控制器展示单个图像,则一切正常。如果我提交了多个图像,则不会显示打印控制器,而是会收到一条错误消息:Warning: Attempt to dismiss from view controller <UIViewController: 0x7fefcc4e7ab0> while a presentation or dismiss is in progress!

下面是有问题的代码。同样,如果printingItems包含超过 1 个元素(这是它的全部点),控制器将不会显示并且success完成处理程序的部分将返回false。在 iOS 12 中不是问题。这是在 iPad 上运行的。

private func print(finalPageImages:[UIImage]) {
    let printInfo = UIPrintInfo(dictionary: nil)
    printInfo.jobName = "job name"
    printInfo.outputType = .general
    printInfo.duplex = .none
    printInfo.orientation = .landscape

    let printController = UIPrintInteractionController.shared
    printController.showsNumberOfCopies = false
    printController.printInfo = printInfo
    printController.printingItems = finalPageImages

    printController.present(from: self.printButton, animated: true) { (controller, success, error) in
        guard error == nil else {
            Utilities.displayAlert(title: "Print Error", msg: error!.localizedDescription, controller: self)
            return
        }
        if success {
            Utilities.displayAlert(title: "Print Status", msg: "Your Shelf Talkers are printing.", controller: self, completion: { (action) in

            })
        } else {
            Utilities.displayAlert(title: "Print Error", msg: "There was a problem with this print job. Please try again.", controller: self)
        }
    }
}
4

1 回答 1

0

我也面临多个 pdf 文件的相同问题,所以我只是将所有 pdf 合并为一个 pdf 并使用

printController.printingItem = mergeAllPdfs().dataRepresentation()

代替

printController.printingItems = [myPdf1.data, myPdf2.data, myPdf3.data]

它现在可以解决这个问题。希望它有所帮助。

func mergeAllPdfs() -> PDFDocument? {
    // get the first pdfData
    guard let firstPdf = allPDFs.first?.data, let pdfDocument = PDFDocument(data: firstPdf) else {
        return nil
    }
    // create new PDFDocument
    let newPdfDocument = PDFDocument()

    // insert all pages of first document
    for p in 0..<pdfDocument.pageCount {
        if let page = pdfDocument.page(at: p) {
            newPdfDocument.insert(page, at: newPdfDocument.pageCount)
        }
    }

    // loop for number of documents instead first one (which is allready added)
    for count in 1..<allPDFs.count {
        guard let nextPdfData = allPDFs[count].data, let nextPdf = PDFDocument(data: nextPdfData) else {
            return nil
        }
        for q in 0..<nextPdf.pageCount {  // all pages of a next document
            if  let page = nextPdf.page(at: q) {
                newPdfDocument.insert(page, at: newPdfDocument.pageCount)
            }
        }
    }
    return newPdfDocument
}
于 2021-12-10T11:41:49.333 回答