Help: How to add UIImage to PDF in Swift

I've been succesful in converting a UITableView to a PDF file. The table view is 627 wide. When creating the PDF from the table view, I've set the PDF page bounds to be twice as large (e.g., 1254).


What I want to do is insert a couple of UIImages into the PDF on the right side of the page (i.e. next to the reproduced table).


I've been searching unsuccessfuly for how to accomplish this.


Any advice would be greatly appreciated.

Replies

Simply draw the image to the destination rectangle you need. The current drawing context should already be set to the PDF's context, so the image will show up there.

Thanks, but a bit more help please.


Here is what I have:


UIGraphicsBeginPDFContextToData(pdfData, pdfPageBounds, nil)
UIGraphicsBeginPDFPageWithInfo(pdfPageBounds, nil)
let pdfContext = UIGraphicsGetCurrentContext()!
statsTable.layer.render(in: pdfContext)


And I've defined the rectangle where I want the UIImage to go:


let playerKillMapRect = CGRect(x: 640, y: 10, width: 330, height: 330)


I've tried this, but it's not working:


pdfContext.draw(image: CGImage, in: CGRect)


I feel like I'm missing something very simple. I've tried converting the UIImage into a CGImage and then calling the above line, but that doesn't work either.


What function call am I missing?


----------------------------


OK, figured it out. And I was missing something very simple:


playerKillMap.draw(in: playerKillMapRect)


Where playerKillMap is the UIImage.


Duh. Thanks for setting me in the right direction.