使用 Swift 打印 iOS 中的视图

Printing the view in iOS with Swift

我正在开发一个应用程序,它需要直接从 iPad 通过 AirPrint 生成和打印访客通行证。

我到处寻找如何打印视图,但我只能找到如何打印文本、webKit 和 mapKit。

有没有办法打印整个视图?如果没有,什么是打印纯文本、框和照片的访客通行证的好解决方案。谢谢

我认为您必须使用 Swift 查看打印照片示例代码: https://developer.apple.com/library/ios/samplecode/PrintPhoto/Introduction/Intro.html

你的view到底是什么,imageView还是UIView?如果您对 imageView 或 UIImage 感兴趣,Apple 的打印照片示例适合您。如果您的主题是 UIView,您可以从 view.layers 创建 pdf 上下文并发送到 AirPrint 功能,如 WebKit、文本,或者您可以打印以创建 pdf 数据。

最好的解决方案是在此处创建 Pdf 文件 swift Generate PDF with Swift

打印 pdf 文件用于 swift 实施:

var pdfLoc = NSURL(fileURLWithPath:yourPdfFilePath)
let printController = UIPrintInteractionController.sharedPrintController()!
let printInfo = UIPrintInfo(dictionary:nil)!

printInfo.outputType = UIPrintInfoOutputType.General
printInfo.jobName = "print Job"
printController.printInfo = printInfo
printController.printingItem = pdfLoc
printController.presentFromBarButtonItem(printButton, animated: true, completionHandler: nil)

我通过修改此处找到的代码找到了问题的答案:

//create an extension to covert the view to an image
extension UIView {
 func toImage() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale)

    drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)

    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
 }
}

//In your view controller    
@IBAction func printButton(sender: AnyObject) {

    let printInfo = UIPrintInfo(dictionary:nil)
    printInfo.outputType = UIPrintInfoOutputType.General
    printInfo.jobName = "My Print Job"

    // Set up print controller
    let printController = UIPrintInteractionController.sharedPrintController()
    printController.printInfo = printInfo

    // Assign a UIImage version of my UIView as a printing iten
    printController.printingItem = self.view.toImage()

    // If you want to specify a printer
    guard let printerURL = URL(string: "Your printer URL here, e.g. ipps://HPDC4A3E0DE24A.local.:443/ipp/print") else { return }
    guard let currentPrinter = UIPrinter(url: printerURL) else { return }                     

    printController.print(to: currentPrinter, completionHandler: nil)

    // Do it
    printController.presentFromRect(self.view.frame, inView: self.view, animated: true, completionHandler: nil)
}

这里Swift3.x

 func prt() {

        let printInfo = UIPrintInfo(dictionary:nil)
        printInfo.outputType = UIPrintInfoOutputType.general
        printInfo.jobName = "My Print Job"

        // Set up print controller
        let printController = UIPrintInteractionController.shared
        printController.printInfo = printInfo

        // Assign a UIImage version of my UIView as a printing iten
        printController.printingItem = self.view.toImage()

        // Do it
        printController.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil)

    }

}

extension UIView {
    func toImage() -> UIImage {
        UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)

        drawHierarchy(in: self.bounds, afterScreenUpdates: true)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

Swift 5:

    let info = UIPrintInfo(dictionary:nil)
    info.outputType = UIPrintInfo.OutputType.general
    info.jobName = "Printing"

    let vc = UIPrintInteractionController.shared
    vc.printInfo = info

    vc.printingItem = UIImage.image(fromView: self.view) // your view here

    vc.present(from: self.view.frame, in: self.view, animated: true, completionHandler: nil)
extension UIImage {

    /// Get image from given view
    ///
    /// - Parameter view: the view
    /// - Returns: UIImage
    public class func image(fromView view: UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0)
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)

        let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return image
    }
}