使用Swift在iOS中打印视图

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

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

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

我通过修改此处的代码找到了我的问题的答案: 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()

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

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...