如何调整正在打印的图像的尺寸?

时间:2019-01-03 13:27:03

标签: swift uiimage uiprintinteractioncntrler

我正在尝试打印由Swift生成的QR码,当我打印它时,它占用了整张纸,我想调整图像的尺寸并缩小它。 Aprox 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 Code Apox 200px x 200px。我目前正在获取A4尺寸的QR码

1 个答案:

答案 0 :(得分:1)

在方法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
}
相关问题