I have a PDF which contains geocoordinates. I'm extracting out that image with the following code (this is for an iOS application):
guard let cgDocument = CGPDFDocument(overlay.pdfUrl as CFURL) else { return }
guard let cgPage = cgDocument.page(at: 1) else { return }
var boundingRect = self.rect(for: overlay.boundingMapRect)
let pdfPageRect = cgPage.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pdfPageRect.size)
var img = renderer.image { ctx in
UIColor.white.set()
ctx.fill(pdfPageRect)
ctx.cgContext.translateBy(x: 0.0, y: pdfPageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(cgPage)
}
Once I have that image, I then need to adjust it to fit the specific coordinate corners. For that, I'm doing the following using a perspectiveTransform:
let ciImg = CIImage(image: img)!
let perspectiveTransformFilter = CIFilter.perspectiveTransform()
perspectiveTransformFilter.inputImage = ciImg
perspectiveTransformFilter.topRight = cartesianForPoint(point: ur, extent: boundingRect)
perspectiveTransformFilter.topLeft = cartesianForPoint(point: ul, extent: boundingRect)
perspectiveTransformFilter.bottomRight = cartesianForPoint(point: lr, extent: boundingRect)
perspectiveTransformFilter.bottomLeft = cartesianForPoint(point: ll, extent: boundingRect)
let txImg = perspectiveTransformFilter.outputImage!
img = UIImage(ciImage: txImg)
The original image is 792 x 612 (a landscape PDF) but the boundingRect covering the coordinates is 25625 x 20030. Obviously when I try to draw the image into that bounding box the app runs out of memory (25625 x 20030 x 4 is around 2GB of memory).
What I'm struggling with is - how do I correctly scale this image to fit into the bounding box even though the bounding box is, oh, 10x the resolution of the actual device? There's some key CoreGraphics thing I'm sure I'm missing here.