微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐