I am trying to create a PDF from a UIView which may contain images, labels, etc.
I have checked the reference ( GeneratingPDF) and I am also aware of the renderInContext: method, which can be used to draw the UIView‘s content onto the PDF context.
I used renderInContext: to create a PDF. However, the images and the text lost their fidelity. i.e. The images were not selectable individually & the text could not be selected/copied, etc. in the resulting PDF. It was as good/bad as the PDF created from a snapshot of the UIView.
My question is, do I have to draw the texts & images individually to achieve what I want (using CGContextShowTextAtPoint & CGContextDrawImage)?
If yes, then how do I go about scanning the UIView for texts & images? Assume that I receive the UIView from outside & am not aware of its contents.
You do indeed have to draw the texts/images individually into a
CGContextRef(look atCGPDFContextCreateWithURL) using functions likeCGContextShowTextAtPoint. While you could theoretically scan your UIView for images/labels, it would be better if you just draw the layout from scratch. You must have some way to know what elements should be in the UIView. Although the code will be very different, logically you should approach it the same way you would if you were going to draw the UIView programmatically instead of loading it from a nib.If you really want to scan your UIView, you could do something like this: