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

Posted

技术标签:

【中文标题】如何调整正在打印的图像的大小?【英文标题】:How to resize an image that is being printed? 【发布时间】:2019-01-03 13:27:03 【问题描述】:

我正在尝试打印 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 尺寸的二维码

【问题讨论】:

您尝试调整哪个代码img ***.com/questions/31314412/… 请尝试更新您的代码。 我已经更新了 【参考方案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

【讨论】:

我现在已经这样做了,但是我收到了错误:参数标签 '(_:, _:)' 与任何可用的重载都不匹配。这是出现在行:让调整大小的图像 很高兴为您提供帮助。 :) 对不起,我刚刚重新运行了代码,图像的大小已经减小,但现在模糊,无法被二维码扫描仪读取 让我们continue this discussion in chat。

以上是关于如何调整正在打印的图像的大小?的主要内容,如果未能解决你的问题,请参考以下文章

调整图像大小而不扭曲它

调整页面内容的大小以适合一个打印页面

如何根据也调整大小的终端宽度打印字符数?

如何调整表单或 FlowLayoutPanel 的大小以打印大于屏幕尺寸?

在单张纸上批量打印多个图像

使用 HTML 和 CSS 打印标签。如何根据标签的高度和宽度调整标签和容器的大小?