Search code examples
swiftuiimageuiprintinteractioncntrler

How to resize an image that is being printed?


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

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
}

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


Solution

  • 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))
    

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

    printController.printingItem = resizedImage
    

    Hope this will help.

    EDIT:

    Below is update code:

    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
    }