I’m having a lot of difficulty finding out how to format the print output of a QRCode image using UIActivityViewController. I can send it to the printer okay, but it prints out at the full width of an A4 sheet. I’m familiar with UISimpleTextPrintFormatter for preparing text to print, but I haven’t found anything specifically for UIImage. Any help or nudges in the right direction much appreciated.
I don’t know how to resize the image, or check for what size it actually is. Here’s the func I use to generate the QRCode and return it as a UIImage. I have tried various resizing functions that people have posted on Stackview, but nothing so far has led to a smaller print out. I’m guessing that there might be some methods within the UIImage class that would resize the image. I’ll do some poking around into that and see what I find.
func qrImage(from string: String) → UIImage? {
var qrcodeImage: CIImage!
let data = string.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "inputMessage")
filter?.setValue("Q", forKey: "inputCorrectionLevel")
qrcodeImage = filter?.outputImage
let image = UIImage(ciImage: qrcodeImage)
return image
I resized the image using the code at the link below, but it still prints to the full width of the A4 page. When I airdrop it it’s only a 10 kb file, so it’s definitely not large.
do you have some settings somewhere that might affect this behavior?
i had a similar experience but on a VB.NET project before, it was taking the print settings of my browser and uses that to print, maybe it also acts the same way.
I don’t think there’s any settings anywhere that would affect it. I’m simply generating the QR Code within the app, on a button press, and then passing the resulting image to UIActivityViewController. Whether I send it as is, or scale it using the linked to code, it remains the same when printing out.
I still haven’t solved this problem, but I’m on the right track I think. I’m taking a look at how to use UIPageRenderer() and a PrintFormatter. Adding text to PrintFormatter is easy, but how to add an image is proving a bit elusive.