如何调整正在打印的图像的大小?

How to resize an image that is being printed?

我正在尝试打印由 Swift 生成的 QR 码,当我打印出来时它占据了整个 sheet 我想调整图像大小并使其变小.大约 200px x 200px

我试过使用不同的 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
}

我期待一个大约 200 像素 x 200 像素的小 QR 码。我目前正在收到 A4 尺寸的 QR 码

由于方法 resizeImage 正在返回新的 UIImage 您需要

的存储结果
self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))

在另一个对象中,例如:

let resizedImage = self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))

现在您可以使用此 resizedImage 图像进行打印,例如:

printController.printingItem = resizedImage

希望这会有所帮助。

编辑:

更新代码如下:

func printImage(img:UIImage) {

    let printController = UIPrintInteractionController.shared

    let printInfo = UIPrintInfo(dictionary:nil)
    printInfo.jobName = "Printing 's QR code"
    printInfo.outputType = .photoGrayscale

    if let resizedImage = self.resizeImage(image: img, targetSize: CGSize(width: 200, height: 200)) {

        printController.printInfo = printInfo
        printController.printingItem = resizedImage

        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 = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
    } else {
        newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
    }

    // This is the rect that we've calculated out and this is what is actually used below
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)

    // Actually do the resizing to the rect using the ImageContext stuff
    UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
    image.draw(in: rect)
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return newImage
}