如何在 Swift 3 中为 UIPrintInfo 打印具有缩放比例的 UIImage?
How to print an UIImage with scale to page for UIPrintInfo in Swift 3?
我有一个 UIView 的子视图如下图所示。
这里我要打印或分享标记-查看。
为了分享,我将标记的UI视图转换为PDF并在电子邮件中分享。它的工作完全符合我的客户的预期。
对于打印,我将标记的UI视图转换为UI图像并打印它。它也可以工作,但我除了必须打印转换后的 UI 图像,并按比例缩放到页面。 但是收据打印太小如下图(爱普生TM-T88V打印机).
我将 UIView 转换为 UIImage 的代码:
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
我的打印图像代码:
@IBAction func act_PrintBtnPressed(_ sender: UIButton) {
m_PrintView.frame.size.height = view.frame.size.height
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.photo
printInfo.jobName = "PrintingReceipt"
printInfo.orientation = .portrait
let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.printingItem = m_PrintView.toImage()
printController.showsNumberOfCopies = false
printController.present(animated: true, completionHandler: nil)
}
最后如何设置份数必须在代码中打印。因为我不想在UI中设置份数,所以我把printController.showsNumberOfCopies
设置为false。
请指导我创建这个。
终于自己找到了。我通过 htmlString 创建了 uicontrols 值。并像下面提到的那样使用 UIPrintPageRenderer。
class TicketPrintPageRenderer: UIPrintPageRenderer {
let receipt: String
init(receipt: String) {
self.receipt = receipt
super.init()
self.headerHeight = 0.0
self.footerHeight = 0.0 // default
let formatter = UIMarkupTextPrintFormatter(markupText: receipt)
formatter.perPageContentInsets = UIEdgeInsets(top: 2, left: 2,
bottom: 2, right: 2)
addPrintFormatter(formatter, startingAtPageAt: 0)
}
}
这里正在为 html 字符串创建控件值,如下所述:
func createHTMLString(ticketDetail: Ticket, QRImage: UIImage) {
let priceStr = String(format: "$%.02f", ticketDetail.price!)
let storeLogoImage: String = self.getImage(imageName: "storeLogo")
let qrCodeImagePath: String = self.getImage(imageName: "qrCode")
printHtmlString = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Document</title><style>body {font-family: sans-serif;}.top_Content {text-align: center;padding-top: 10px;}.margin_8 {margin: 8px auto;}.fontstyle_normal {font-weight: normal;}.float_left {float: left;}.float_right {float: right;}.vl {border-left: 2px solid rgb(0, 0, 0);height: 40px;position: absolute;left: 50%;}.container_body{text-align: center;font-weight: normal;}.squareBackground {background-color: rgb(162, 161, 159);width: 100px;height: 100px;}</style></head><body><div><div class=\"top_Content\"><img src=\"file://\(storeLogoImage)\" alt=\"laundryboss_logo\" height=\"100px\"><h3 class=\"margin_8 fontstyle_normal\">\(EmployeeModel.sharedInstance.currentEmployee.employeeSelectedLocation.customerName!)</h3><h4 class=\"margin_8 fontstyle_normal\">\(EmployeeModel.sharedInstance.currentEmployee.employeeSelectedLocation.locationName!)</h4><h3 style=\"width:40%; padding-left: 17px; font-size:1em;\" class=\"float_left margin_8 fontstyle_normal\">Date : \((self.printShareViewModel?.splitDateandTime(createdAt: ticketDetail.ticketDate).createdDate)!)</h3><!-- <span>❘</span> --><div class=\"vl\"></div><h3 style=\"width:40%; padding-right: 17px; font-size:1em;\" class=\"float_right margin_8 fontstyle_normal\">Time : \((self.printShareViewModel?.splitDateandTime(createdAt: ticketDetail.ticketDate).createdTime)!)</h3><h2 style=\"margin: 0;clear: both;padding-top: 14px; font-size:1.2em;\" class=\"fontstyle_normal\">Ticket #\(ticketDetail.ticketNumber!)</h2><h2 style=\"margin: 0;clear: both;padding-top: 14px; font-size:1.2em;\" class=\"fontstyle_normal\">\(ticketDetail.user.name!)</h2><div style=\"display: table;margin: 0 auto;padding-top: 16px;\"><h2 style=\"margin: 0; font-size:1.2em;\" class=\"float_left fontstyle_normal\">Phone :</h2><h3 style=\"margin: 0; font-size:1.2em; padding-left:6px;\" class=\"float_left fontstyle_normal\">\(ticketDetail.user.phone!)</h3></div><p style=\"margin: 0;padding-top: 16px; font-size:1.2em;\">\(ticketDetail.notes!)</p><div style=\"display: table;margin: 0 auto;padding-top: 16px;\"><h2 style=\"margin: 0; font-size:1.2em;\" class=\"float_left fontstyle_normal\">Price :</h2><h3 style=\"margin: 0; font-size:1.2em; padding-left:6px;\" class=\"float_left fontstyle_normal\">\(priceStr)</h3></div><div style=\"margin: 0 auto;display: table; margin-top: 16px;\" class=\"squareBackground\"><!-- <div class=\"squareBackground\"> --><img src=\"file://\(qrCodeImagePath)\" alt=\"Qr_code\" width=\"90\" height=\"90\" align=\"middle\" style=\"padding:5px;\"><!-- </div> --></div></div></div></body></html>"
}
现在当用户点击打印按钮时:
@IBAction func act_PrintBtnPressed(_ sender: UIButton) {
let formatter = TicketPrintPageRenderer(receipt: printHtmlString)
let printController = UIPrintInteractionController.shared
printController.showsNumberOfCopies = true
printController.showsPageRange = false
printController.printPageRenderer = formatter
printController.present(animated: true) { (controller, success, errorMsg) in
if success {
print("*************** Print Successfully")
} else {
print("*************** Print Failed : \(errorMsg?.localizedDescription)")
}
}
}
符合我排除的条件。
我有一个 UIView 的子视图如下图所示。
这里我要打印或分享标记-查看。
为了分享,我将标记的UI视图转换为PDF并在电子邮件中分享。它的工作完全符合我的客户的预期。
对于打印,我将标记的UI视图转换为UI图像并打印它。它也可以工作,但我除了必须打印转换后的 UI 图像,并按比例缩放到页面。 但是收据打印太小如下图(爱普生TM-T88V打印机).
我将 UIView 转换为 UIImage 的代码:
extension UIView {
func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
我的打印图像代码:
@IBAction func act_PrintBtnPressed(_ sender: UIButton) {
m_PrintView.frame.size.height = view.frame.size.height
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.outputType = UIPrintInfoOutputType.photo
printInfo.jobName = "PrintingReceipt"
printInfo.orientation = .portrait
let printController = UIPrintInteractionController.shared
printController.printInfo = printInfo
printController.printingItem = m_PrintView.toImage()
printController.showsNumberOfCopies = false
printController.present(animated: true, completionHandler: nil)
}
最后如何设置份数必须在代码中打印。因为我不想在UI中设置份数,所以我把printController.showsNumberOfCopies
设置为false。
请指导我创建这个。
终于自己找到了。我通过 htmlString 创建了 uicontrols 值。并像下面提到的那样使用 UIPrintPageRenderer。
class TicketPrintPageRenderer: UIPrintPageRenderer {
let receipt: String
init(receipt: String) {
self.receipt = receipt
super.init()
self.headerHeight = 0.0
self.footerHeight = 0.0 // default
let formatter = UIMarkupTextPrintFormatter(markupText: receipt)
formatter.perPageContentInsets = UIEdgeInsets(top: 2, left: 2,
bottom: 2, right: 2)
addPrintFormatter(formatter, startingAtPageAt: 0)
}
}
这里正在为 html 字符串创建控件值,如下所述:
func createHTMLString(ticketDetail: Ticket, QRImage: UIImage) {
let priceStr = String(format: "$%.02f", ticketDetail.price!)
let storeLogoImage: String = self.getImage(imageName: "storeLogo")
let qrCodeImagePath: String = self.getImage(imageName: "qrCode")
printHtmlString = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\"><title>Document</title><style>body {font-family: sans-serif;}.top_Content {text-align: center;padding-top: 10px;}.margin_8 {margin: 8px auto;}.fontstyle_normal {font-weight: normal;}.float_left {float: left;}.float_right {float: right;}.vl {border-left: 2px solid rgb(0, 0, 0);height: 40px;position: absolute;left: 50%;}.container_body{text-align: center;font-weight: normal;}.squareBackground {background-color: rgb(162, 161, 159);width: 100px;height: 100px;}</style></head><body><div><div class=\"top_Content\"><img src=\"file://\(storeLogoImage)\" alt=\"laundryboss_logo\" height=\"100px\"><h3 class=\"margin_8 fontstyle_normal\">\(EmployeeModel.sharedInstance.currentEmployee.employeeSelectedLocation.customerName!)</h3><h4 class=\"margin_8 fontstyle_normal\">\(EmployeeModel.sharedInstance.currentEmployee.employeeSelectedLocation.locationName!)</h4><h3 style=\"width:40%; padding-left: 17px; font-size:1em;\" class=\"float_left margin_8 fontstyle_normal\">Date : \((self.printShareViewModel?.splitDateandTime(createdAt: ticketDetail.ticketDate).createdDate)!)</h3><!-- <span>❘</span> --><div class=\"vl\"></div><h3 style=\"width:40%; padding-right: 17px; font-size:1em;\" class=\"float_right margin_8 fontstyle_normal\">Time : \((self.printShareViewModel?.splitDateandTime(createdAt: ticketDetail.ticketDate).createdTime)!)</h3><h2 style=\"margin: 0;clear: both;padding-top: 14px; font-size:1.2em;\" class=\"fontstyle_normal\">Ticket #\(ticketDetail.ticketNumber!)</h2><h2 style=\"margin: 0;clear: both;padding-top: 14px; font-size:1.2em;\" class=\"fontstyle_normal\">\(ticketDetail.user.name!)</h2><div style=\"display: table;margin: 0 auto;padding-top: 16px;\"><h2 style=\"margin: 0; font-size:1.2em;\" class=\"float_left fontstyle_normal\">Phone :</h2><h3 style=\"margin: 0; font-size:1.2em; padding-left:6px;\" class=\"float_left fontstyle_normal\">\(ticketDetail.user.phone!)</h3></div><p style=\"margin: 0;padding-top: 16px; font-size:1.2em;\">\(ticketDetail.notes!)</p><div style=\"display: table;margin: 0 auto;padding-top: 16px;\"><h2 style=\"margin: 0; font-size:1.2em;\" class=\"float_left fontstyle_normal\">Price :</h2><h3 style=\"margin: 0; font-size:1.2em; padding-left:6px;\" class=\"float_left fontstyle_normal\">\(priceStr)</h3></div><div style=\"margin: 0 auto;display: table; margin-top: 16px;\" class=\"squareBackground\"><!-- <div class=\"squareBackground\"> --><img src=\"file://\(qrCodeImagePath)\" alt=\"Qr_code\" width=\"90\" height=\"90\" align=\"middle\" style=\"padding:5px;\"><!-- </div> --></div></div></div></body></html>"
}
现在当用户点击打印按钮时:
@IBAction func act_PrintBtnPressed(_ sender: UIButton) {
let formatter = TicketPrintPageRenderer(receipt: printHtmlString)
let printController = UIPrintInteractionController.shared
printController.showsNumberOfCopies = true
printController.showsPageRange = false
printController.printPageRenderer = formatter
printController.present(animated: true) { (controller, success, errorMsg) in
if success {
print("*************** Print Successfully")
} else {
print("*************** Print Failed : \(errorMsg?.localizedDescription)")
}
}
}
符合我排除的条件。