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