使用Swift在iOS中打印视图

时间:2015-12-10 15:51:28

标签: ios swift printing airprint

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

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

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

4 个答案:

答案 0 :(得分:10)

我通过修改此处的代码找到了我的问题的答案:AirPrint contents of a UIView

//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)
}

答案 1 :(得分:3)

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

您的观点,imageView或UIView究竟是什么?如果您对imageView或UIImage感兴趣,可以使用Apple的Print Photo样本。如果您的主题是UIView,您可以从view.layers创建pdf上下文并发送到AirKrint func,如WebKit,文本,或者您可以打印以创建pdf数据。

最好的解决方案是Create Pdf文件在这里用于swift Generate PDF with Swift

打印pdf文件是为了快速实施:

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)

答案 2 :(得分:1)

Swift 3.x中的Hier

 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!
    }
}

答案 3 :(得分:0)

迅速4:

        let printInfo = UIPrintInfo(dictionary:nil)
        printInfo.outputType = UIPrintInfo.OutputType.general
        printInfo.jobName = "iOS app printing"

        let printController = UIPrintInteractionController.shared
        printController.printInfo = printInfo

        printController.printingItem = UIImage.imageFromView(self.view) // your view here

        printController.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 imageFromView(view: UIView) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(view.frame.size, false, 0)
        view.drawHierarchy(in: view.bounds, afterScreenUpdates: false)

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