我正在尝试打印由 Swift 生成的 QR 码,当我打印它时它占用了整张我想要调整图像大小并使其更小的工作表。大约 200 像素 x 200 像素
我尝试过使用不同的 UIImage 调整大小功能,但结果仍然相同。我也尝试使用 printController 调整图像大小
func printImage(img:UIImage) {
let printController = UIPrintInteractionController.shared
let printInfo = UIPrintInfo(dictionary:nil)
printInfo.jobName = "Printing \(myTextField.text) 's QR code"
printInfo.outputType = .photoGrayscale
self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))
printController.printInfo = printInfo
printController.printingItem = img
printController.present(animated: true) { (_, isPrinted, error) in
if error == nil {
if isPrinted {
print("QR Code Has Been Printed :)")
} else {
print("QR Code Not Printed")
}
}
}
}
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSizeMake(size.width * heightRatio, size.height * heightRatio)
} else {
newSize = CGSizeMake(size.width * widthRatio, size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
我期待一个小的 QR 码 Aprox 200 像素 x 200 像素。我目前正在获取 A4 尺寸的二维码