Draw NSData to PDF?

In my app, I print some images to a pdf using PDFKit. My pdf's are very big because of the size of the images. I looked up how to compress the images to a smaller size so that the PDF wouldn't be so large. The problem is that this compression returns as NSData. NSData has no .draw method though.

Currently I have a UIImage (for example: imageVar)... I simply write: imageVar.draw(in: photoRect) and it prints to the PDF with the dimensions I previously declared.

When I compress the image and then try to print it out: resizedImage.draw(in: photoRect)
it doesn't work. "Value of type 'NSData' has no member 'draw'"

How do I "draw" this compressed image to the PDF???

Thanks

An NSData (or Data) isn't an image. It can encode an image, but it could also be a text file, or any other arbitrary binary data.

I don't know what kind of compression you did to your image, but if it was something like a zip, 7z, etc. type compression (an algorithm that takes any arbitrary data input, and spits it out as compressed Data), then that's not something you can include in a PDF.

What you'll need is an image compression algorithm, whose input is specifically an image, which does compression on it (e.g. DCT as seen in jpeg), and results in a new valid image with reduced quality and size.

For example, take a look at UIImage.jpegData(compressionQuality:). It operates on a UIImage, and uses JPEG compression to produce a lower quality result, encoded as Data. I'm not toooo familiar with UIKit, but it looks like since Data still encodes a valid image (obviously, which just created it as such), it looks like it can be used to initialize a new UIImage. IDK what kind of representation UIImage uses internally, but hopefully it's one that can use this new lowered file size.

Interesting. Okay. Can you supply me with a link to a tutorial or StackOverflow? All that I seem to be finding is the wrong kind of compression apparently. It would be a huge help!

Not really, I just read a tiny bit out of the official documentation.

I'm curious, where did you find this .draw(in:) method?

I’m a beginner, so I mostly just follow tutorials. I followed this one for the PDF:

https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit

Fundamentally, draw-ing an image into an image context effectively uncompresses it by decoding the compressed image into pixel values to draw out. So even if could take the NSData you got out of the compression algorithm and then figure out how to draw it into the PDF context, you'd be undoing the compression you started with in the previous step.

What you'd need to do is look into how PDFKit handles image compression within itself. Alas, I'm not familiar with how PDFKit works but it seems like it acts as it's own drawing context? If that's the case, then you'd need to see if there are compression options within that library.

EDIT: A quick search in the PDFKit API shows that there is a configuration option to set up compression on the output:

Thank you for your help!

You've linked to PSPDFKit documentation. PSPDFKit is a third-party commercial library. It's not the same as Apple's PDFKit.

Doh! You're right. Before I made my edit I figured I'd do a quick google search and I completely misread the original post as PSPDFKit. My bad.

That said, I'm not aware of any public API to set the image compression in PDFKit.