I'm converting a PDF page into a UIImage. While doing so, I lose the image quality. Need help in getting high quality images.
Code to generate UIImage
-(UIImage *)imageForPage:(int)pageNumber {
CGPDFPageRef pdfPageRef = [self pdfReferenceForPage:pageNumber];
CGSize pageSize = [self sizeOfPage:pageNumber];
//UIGraphicsBeginImageContext(pageSize);
UIGraphicsBeginImageContextWithOptions(pageSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextTranslateCTM(context, 0.0, pageSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pdfPageRef, kCGPDFCropBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, pdfPageRef);
CGContextRestoreGState(context);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
- (void)saveImage {
UIImage *image = [self imageForPage:1];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:filePath atomically:YES];
}
Image comparison
Original PDF
Image from PDF
EDIT: CODE WITH CHANGES
float dpi = 100.0 / 72.0;
CGPDFPageRef pdfPageRef = [self pdfReferenceForPage:pageNumber];
CGSize pageSize = [self sizeOfPage:pageNumber];
pageSize.width = pageSize.width * dpi;
pageSize.height = pageSize.height * dpi;
UIGraphicsBeginImageContext(pageSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextTranslateCTM(context, 0.0, pageSize.height);
CGContextScaleCTM(context, dpi, -dpi);
CGContextSaveGState(context);
//CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pdfPageRef, kCGPDFCropBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true);
//CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, pdfPageRef);
CGContextRestoreGState(context);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
When you ask the PDF for it's page size, you're getting a width/height for 72 PPI. You might try creating a context that's scaled up using a scale transform. For example, if you wanted to render at 300 dpi, add a scale transform to scale by 300.0/72.0
.
If you export as TIFF, you will be able to encapsulate the final PPI (300) of the generated image.
This one gives the best quality... if you need another quality just change dpi.
func drawPDFfromURL(url: URL) -> UIImage? {
guard let document = CGPDFDocument(url as CFURL) else { return nil }
guard let page = document.page(at: 1) else { return nil }
let dpi: CGFloat = 300.0 / 72.0
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: CGSize(width: pageRect.size.width * dpi, height: pageRect.size.height * dpi))
let img1 = renderer.jpegData(withCompressionQuality: 1.0, actions: { cnv in
UIColor.white.set()
cnv.fill(pageRect)
cnv.cgContext.translateBy(x: 0.0, y: pageRect.size.height * dpi);
cnv.cgContext.scaleBy(x: dpi, y: -dpi);
cnv.cgContext.drawPDFPage(page);
})
let img2 = UIImage(data: img1)
return img2
}
Try this code for Swift 3 :
func drawPDFfromURL(url: URL) -> UIImage? {
guard let document = CGPDFDocument(url as CFURL) else { return nil }
guard let page = document.page(at: 1) else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
let img1 = renderer.jpegData(withCompressionQuality: 1.0, actions: { cnv in
UIColor.white.set()
cnv.fill(pageRect)
cnv.cgContext.translateBy(x: 0.0, y: pageRect.size.height);
cnv.cgContext.scaleBy(x: 1.0, y: -1.0);
cnv.cgContext.drawPDFPage(page);
})
let img2 = UIImage(data: img1)
return img2
}
This gives much higher resolution
I've written a handy UIImage PDF renderer category that you may find helpful. It also caches all rendered results for high performance.
https://github.com/mindbrix/UIImage-PDF
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With