有没有办法将PDF页面渲染到位图上下文而不会降低质量?
这是我的代码:
-(UIImage *)imageForPage:(int)pageNumber sized:(CGSize)size{
CGPDFDocumentRef _document = [self CreatePDFDocumentRef:filePath];
CGPDFPageRef page = CGPDFDocumentGetPage (_document, pageNumber);
CGRect pageRect = CGPDFPageGetBoxRect(page,kCGPDFTrimBox);
CGSize pageSize = pageRect.size;
pageSize = MEDSizeScaleAspectFit(pageSize, size);
UIGraphicsBeginImageContextWithOptions(pageSize, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextTranslateCTM(context, 0.0, pageSize.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSaveGState(context);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFTrimBox, CGRectMake(0, 0, pageSize.width, pageSize.height), 0, true);
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGPDFDocumentRelease (_document);
return resultingImage;
}
CGSize MEDSizeScaleAspectFit(CGSize size, CGSize maxSize) {
CGFloat originalAspectRatio = size.width / size.height;
CGFloat maxAspectRatio = maxSize.width / maxSize.height;
CGSize newSize = maxSize;
// The largest dimension will be the `maxSize`, and then we need to scale
// the other dimension down relative to it, while maintaining the aspect
// ratio.
if (originalAspectRatio > maxAspectRatio) {
newSize.height = maxSize.width / originalAspectRatio;
} else {
newSize.width = maxSize.height * originalAspectRatio;
}
return newSize;
}
答案 0 :(得分:0)
确保您创建的图像接近等于其显示的尺寸。这将创建一个最准确显示的图像。