【问题标题】:Poor quality of CFGPDF rendered in bitmap context在位图上下文中呈现的 CFGPDF 质量较差
【发布时间】:2014-01-07 19:58:50
【问题描述】:

有没有办法在不损失质量的情况下将 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;
}

【问题讨论】:

  • 你传递给这个函数的尺寸是多少,你在屏幕上显示的尺寸是多少?这是我发现的图像清晰度的两个最大因素(如果您期望的图像质量比您所拥有的更好)。
  • 从 Putz1103 开始​​,如果您可以评论您认为图像质量有什么问题,那将会很有帮助。您作为示例发布的内容如何改进?
  • 关于质量,与原始 pdf 文件相比,文本模糊。关于大小,我更改了生成图像的大小以完全适合其目标(1024 x 600),我可以看到重大改进。现在看起来很棒。

标签: ios core-graphics cgpdfdocument cgpdf


【解决方案1】:

确保您正在创建的图像与将要显示的大小接近。这将创建一个在视觉上最准确的图像。

【讨论】:

    猜你喜欢
    • 2011-02-01
    • 2021-12-31
    • 2016-12-29
    • 2016-08-09
    • 1970-01-01
    • 2016-05-05
    • 2016-04-07
    • 1970-01-01
    • 2013-11-11
    相关资源
    最近更新 更多