【问题标题】:Generate hi resolution pdf on ios在ios上生成高分辨率pdf
【发布时间】:2013-02-23 08:05:44
【问题描述】:

有什么方法可以在支持视网膜显示的 ios 上生成高分辨率 PDF?我制作了一个基本的 PDF 生成器,但在我的 iPad 3 上它看起来仍然像素化。感谢您的各种建议!

更新:

在我的 PDF 文件中,文本流畅美观。但是当我使用代码来绘制这个 pdf 时,我在视网膜显示器上被像素化了(绘图代码在底部)。

PDF 生成:

    NSString *fileName = [self.bookManager cacheBookFileForPage:i];
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    CGContextTranslateCTM(currentContext, 0, 100);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // TODO: Temporary numbers
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 768 * 2, 1024 * 2), nil);


    NSString *textToDraw = @"text";
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;

    CTFontRef ctFont= CTFontCreateWithName(CFSTR("Arial"), 20 * 2, &CGAffineTransformIdentity);

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                (__bridge id)ctFont, (NSString *)kCTFontAttributeName,
                                nil];

    NSAttributedString *string = [[NSAttributedString alloc] initWithString:textToDraw
                                                                 attributes:attributes];

    CFRelease(ctFont);


    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)string);

    // TODO: temporary        
    CGRect frameRect = CGRectMake(100, 100, 800 * 2, 50 * 2);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();

PDF 绘图:

// This will return pageRef from PDF
CGPDFPageRef page = [self.bookManager contentForPage:index + 1];

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));
CGContextTranslateCTM(context, 0.0f, [self.view.layer bounds].size.height);

CGRect transformRect = CGRectMake(0, 0, [self.view.layer bounds].size.width, [self.view.layer bounds].size.height);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, transformRect, 0, true);

// And apply the transform.
CGContextConcatCTM(context, pdfTransform);

CGContextScaleCTM(context, 1.0f, -1.0f);

CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);

CGContextDrawPDFPage(context, page);

【问题讨论】:

  • 您的文档中有哪些内容?如果您的图像包含锯齿,那么您需要提高它们的分辨率。如果您的字体是像素化的,那么您可能没有使用基于矢量的字体——您使用的是什么字体?您是如何生成 PDF 的?
  • @halfer 你是对的。这个问题需要一些代码:)
  • 一个更好的问题。好的,如果您在台式机上查看生成的 PDF,您是否会在高缩放级别下看到锯齿状字体?如果不是,那么问题出在您的显示代码中。 (不是 iOS 程序员,但现在您的问题中有代码,有人可以帮助您)。

标签: ios pdf retina-display


【解决方案1】:

我认为您遇到的问题之一是您正在缩放您正在绘制的内容;不要使用那个变换!只需创建您的 pdf 上下文并在其中进行本地绘制。你的字体看起来很清晰,因为它们现在以全分辨率绘制,而不是半分辨率,然后该字体的位图被放大,这当然会引入锯齿。

tl;dr:直接绘制到 pdf,不要对文本使用缩放变换。

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 2014-08-05
    • 1970-01-01
    • 2013-02-06
    • 2012-01-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    相关资源
    最近更新 更多