Learn Courses My Dashboard

Prepping UIImage for printing (a QRCode)

Hi all,

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 thimk the problem is the sheer size of the image (dimensions)… Have you tried printing a smaller dimension image and see how big or small it prints?

Im guessing there is some sort of width and height ceiling for every paper size

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.

can you show your code for the printing?

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.

let shareSheet = UIActivityViewController(activityItems: [scaledImage], applicationActivities: nil)
present(shareSheet, animated: true, completion: nil)

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.