加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Swift在iOS中打印视图

发布时间:2020-12-14 05:46:09 所属栏目:百科 来源:网络整理
导读:我正在开发一个应用程序,需要通过AirPrint直接从iPad生成和打印访问者通行证. 我到处寻找如何打印视图,但我只能找到如何打印文本,webKit和mapKit. 有没有办法打印整个视图?如果没有,打印访客通行证将是一个很好的解决方案,它将是纯文本,方框和照片.谢谢. 我
我正在开发一个应用程序,需要通过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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读