如何以更高分辨率将 PDF 绘制到 CGContext?

发布于 2024-11-27 20:45:49 字数 1327 浏览 2 评论 0原文

我编写了一个视图来渲染 PDF 页面:

-(UIPDFRenderView*) initWithFrame:(CGRect)frame withDocument:(CGPDFDocumentRef*)document withPageNumber:(int)pageNumber
{        
    if (self) 
    {
        page = CGPDFDocumentGetPage (*document, pageNumber);

        pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        pdfScale = (CGRectGetWidth(frame)/pageRect.size.width);

        rect = frame;
        self = [super initWithFrame:CGRectMake(CGRectGetMinX(frame),
                                               CGRectGetMinY(frame),
                                               CGRectGetWidth(frame), 
                                               CGRectGetHeight(pageRect) * pdfScale)];
    }
    return  self;
}


-(void) drawRect:(CGRect)rect
{
    [self displayPDFPageWithContext:UIGraphicsGetCurrentContext()];    
}

-(void) displayPDFPageWithContext:(CGContextRef)context
{
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context, self.bounds);

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, pdfScale,-1*pdfScale);

    CGContextDrawPDFPage (context, page);
}

我对 pdf 文档的每一页使用该视图之一,并将它们添加到 acrollview 中。 问题是如果我放大,页面是像素化的。毫不奇怪,因为我正在使用屏幕上下文来渲染 pdf。是否有可能以双倍分辨率渲染该 pdf,以便放大至 2 倍?

I wrote a view to render a PDF Page:

-(UIPDFRenderView*) initWithFrame:(CGRect)frame withDocument:(CGPDFDocumentRef*)document withPageNumber:(int)pageNumber
{        
    if (self) 
    {
        page = CGPDFDocumentGetPage (*document, pageNumber);

        pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        pdfScale = (CGRectGetWidth(frame)/pageRect.size.width);

        rect = frame;
        self = [super initWithFrame:CGRectMake(CGRectGetMinX(frame),
                                               CGRectGetMinY(frame),
                                               CGRectGetWidth(frame), 
                                               CGRectGetHeight(pageRect) * pdfScale)];
    }
    return  self;
}


-(void) drawRect:(CGRect)rect
{
    [self displayPDFPageWithContext:UIGraphicsGetCurrentContext()];    
}

-(void) displayPDFPageWithContext:(CGContextRef)context
{
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context, self.bounds);

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
    CGContextScaleCTM(context, pdfScale,-1*pdfScale);

    CGContextDrawPDFPage (context, page);
}

I use one of that view for every page of a pdf document and add them to a acrollview.
The problem is if i zoom in, the page is pixelig. No surprise because i am using the screen context to render the pdf. Is there any posibility to render that pdf with the double resolution to enable zoomin to a factor of 2?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风向决定发型 2024-12-04 20:45:49

好的,我得到了解决方案,我以原始大小渲染它,并以低比例启动 scollview,使 pdf 的宽度适合滚动视图的宽度。使用该方法我可以缩放到原始大小(比例为1)。

Okay i got the solution, i render it in original size and initiate the scollview with a low scale that the width of the pdf fits with the width of the scrollview. With that methode i can zoom till original size (scale of 1).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文