使用Swift在iOS中打印视图

前端之家收集整理的这篇文章主要介绍了使用Swift在iOS中打印视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个应用程序,需要通过AirPrint直接从iPad生成和打印访问者通行证.

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

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

我通过修改此处的代码找到了我的问题的答案: AirPrint contents of a UIView
  1. //create an extension to covert the view to an image
  2. extension UIView {
  3. func toImage() -> UIImage {
  4. UIGraphicsBeginImageContextWithOptions(bounds.size,false,UIScreen.mainScreen().scale)
  5.  
  6. drawViewHierarchyInRect(self.bounds,afterScreenUpdates: true)
  7.  
  8. let image = UIGraphicsGetImageFromCurrentImageContext()
  9. UIGraphicsEndImageContext()
  10. return image
  11. }
  12. }
  13.  
  14. //In your view controller
  15. @IBAction func printButton(sender: AnyObject) {
  16.  
  17. let printInfo = UIPrintInfo(dictionary:nil)
  18. printInfo.outputType = UIPrintInfoOutputType.General
  19. printInfo.jobName = "My Print Job"
  20.  
  21. // Set up print controller
  22. let printController = UIPrintInteractionController.sharedPrintController()
  23. printController.printInfo = printInfo
  24.  
  25. // Assign a UIImage version of my UIView as a printing iten
  26. printController.printingItem = self.view.toImage()
  27.  
  28. // Do it
  29. printController.presentFromRect(self.view.frame,inView: self.view,animated: true,completionHandler: nil)
  30. }

猜你在找的Swift相关文章