如何调整正在打印的图像的大小? [英] How to resize an image that is being printed?

查看:35
本文介绍了如何调整正在打印的图像的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印由 Swift 生成的二维码,当我打印它时,它占据了整张纸,我想调整图像大小并使其变小.大约 200 像素 x 200 像素

I am trying to print A QR Code that has been generated by Swift when I print it out it is taking up the whole sheet I am wanting to resize the image and make it smaller. Aprox 200px x 200px

我尝试使用不同的 UIImage 调整大小功能,但结果仍然相同.我还尝试使用 printController 调整图像大小

I have tried using different UIImage resize functions and is still coming out the same. I have also tried to resize the image with the 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 像素的小二维码.我目前正在收到一个 A4 尺寸的二维码

I am expecting a small QR Code Aprox 200px by 200px. I am currently getting a A4 Size QR Code

推荐答案

As method resizeImage is Returning new UIImage you need store result of

As method resizeImage is returning new UIImage you need store result of

self.resizeImage(img, targetSize: CGSizeMake(200.0, 200.0))

在另一个对象中,例如:

in another object like:

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

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

And now you can use this resizedImage image for print like:

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
}

这篇关于如何调整正在打印的图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆