当比例 > 时渲染 PDF 时出现白色边框1

发布于 2024-11-09 20:01:06 字数 1135 浏览 1 评论 0原文

我有一个从 CGPDFPageRef 返回 UIImage 的方法。您可以指定图像的宽度。

问题是当 pdfScale > 时1、图像中出现白色边框。因此 PDF 始终以带有边框的比例 1 绘制,而不是更大的比例。较小的比例是可以的。

我尝试更改 PDFBox 的类型,但这似乎没有改变任何内容,而且文档也不是很清楚。

有人看到错误吗?

- (UIImage*) PDFImageForWidth:(CGFloat) width {
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    CGFloat pdfScale = width/pageRect.size.width;
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
    pageRect.origin = CGPointZero;

    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context, pageRect);

    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true));

    CGContextDrawPDFPage(context, page);
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

I have a method that return an UIImage from a CGPDFPageRef. You can specify a width for the image.

The problem is that when pdfScale is > 1, a white border appears in the image. So the PDF is always drawn at scale 1 with a border instead of a bigger scale. Smaller scales are OK.

I've tried to change the type of PDFBox but that doesn't seems to change anything and the documentation is not really clear.

Does somebody sees the error?

- (UIImage*) PDFImageForWidth:(CGFloat) width {
    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    CGFloat pdfScale = width/pageRect.size.width;
    pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale);
    pageRect.origin = CGPointZero;

    UIGraphicsBeginImageContext(pageRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
    CGContextFillRect(context, pageRect);

    CGContextTranslateCTM(context, 0.0, pageRect.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true));

    CGContextDrawPDFPage(context, page);
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

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

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

发布评论

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

评论(2

生活了然无味 2024-11-16 20:01:06

问题是当缩放 > CGPDFPageGetDrawingTransform 时,CGPDFPageGetDrawingTransform 不会缩放 PDF。 1.你必须自己做。

此外,该方法现在是线程安全的。

- (UIImage*) PDFImageForWidth:(CGFloat) width {
    CGRect smallPageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    CGFloat pdfScale = width/smallPageRect.size.width;

    CGRect pageRect;
    pageRect.size = CGSizeMake(smallPageRect.size.width*pdfScale, smallPageRect.size.height*pdfScale);
    pageRect.origin = CGPointZero;

    __block CGContextRef context;
    dispatch_sync(dispatch_get_main_queue(), ^{
        UIGraphicsBeginImageContext(pageRect.size);
        context = UIGraphicsGetCurrentContext();
    });

    if (context != nil) {
        CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
        CGContextFillRect(context, pageRect);

        CGContextTranslateCTM(context, 0.0, pageRect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true);
        if (pdfScale > 1) {
            transform = CGAffineTransformScale(transform, pdfScale, pdfScale);
            transform.tx = 0;
            transform.ty = 0;
        }
        CGContextConcatCTM(context, transform);

        CGContextDrawPDFPage(context, page);

        __block UIImage* image;
        dispatch_sync(dispatch_get_main_queue(), ^{
            image = [UIGraphicsGetImageFromCurrentImageContext() retain];
            UIGraphicsEndImageContext();
        });

        return [image autorelease];  
    }
    else return nil;
}

The problem was that CGPDFPageGetDrawingTransform doesn't scale the PDF when scale > 1. You have to do it yourself.

Also, the method is thread safe now.

- (UIImage*) PDFImageForWidth:(CGFloat) width {
    CGRect smallPageRect = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    CGFloat pdfScale = width/smallPageRect.size.width;

    CGRect pageRect;
    pageRect.size = CGSizeMake(smallPageRect.size.width*pdfScale, smallPageRect.size.height*pdfScale);
    pageRect.origin = CGPointZero;

    __block CGContextRef context;
    dispatch_sync(dispatch_get_main_queue(), ^{
        UIGraphicsBeginImageContext(pageRect.size);
        context = UIGraphicsGetCurrentContext();
    });

    if (context != nil) {
        CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
        CGContextFillRect(context, pageRect);

        CGContextTranslateCTM(context, 0.0, pageRect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, pageRect, 0, true);
        if (pdfScale > 1) {
            transform = CGAffineTransformScale(transform, pdfScale, pdfScale);
            transform.tx = 0;
            transform.ty = 0;
        }
        CGContextConcatCTM(context, transform);

        CGContextDrawPDFPage(context, page);

        __block UIImage* image;
        dispatch_sync(dispatch_get_main_queue(), ^{
            image = [UIGraphicsGetImageFromCurrentImageContext() retain];
            UIGraphicsEndImageContext();
        });

        return [image autorelease];  
    }
    else return nil;
}
提赋 2024-11-16 20:01:06

我遇到了同样的问题,之前的答案确实在某些方面帮助了我,但并没有解决整个问题。

这就是我解决问题的方法。

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    NSLog(@"pdfQuartzViewViewController - drawLayer");

    CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:self.strFilename]);
    CGPDFPageRef pageReference = CGPDFDocumentGetPage(myDocumentRef, 1);

    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White
    CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill

    CGContextTranslateCTM(context, 0.0f, layer.bounds.size.height);

    // Scaling the pdf page a little big bigger than the view so we can cover the white borders on the page
    CGContextScaleCTM(context, 1.0015f, -1.0015f);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageReference, kCGPDFCropBox, layer.bounds, 0, true);
    pdfTransform.tx = pdfTransform.tx - 0.25f;
    pdfTransform.ty = pdfTransform.ty - 0.40f;

    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, pageReference); // Render the PDF page into the context

    CGPDFDocumentRelease(myDocumentRef);
    myDocumentRef = NULL;
}

这里有一个很好的链接,您可以在其中找到有关它的更多信息。这篇文章解释了如何使用所有这些方法。

http://iphonedevelopment.blogspot.ch/2008/10/demystifying-cgaffinetransform.html

I was having the same problem and the answer before did help me in some parts but it didn't solve the whole problem.

This is the how I solved my problem.

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
    NSLog(@"pdfQuartzViewViewController - drawLayer");

    CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL fileURLWithPath:self.strFilename]);
    CGPDFPageRef pageReference = CGPDFDocumentGetPage(myDocumentRef, 1);

    CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White
    CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill

    CGContextTranslateCTM(context, 0.0f, layer.bounds.size.height);

    // Scaling the pdf page a little big bigger than the view so we can cover the white borders on the page
    CGContextScaleCTM(context, 1.0015f, -1.0015f);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageReference, kCGPDFCropBox, layer.bounds, 0, true);
    pdfTransform.tx = pdfTransform.tx - 0.25f;
    pdfTransform.ty = pdfTransform.ty - 0.40f;

    CGContextConcatCTM(context, pdfTransform);

    CGContextDrawPDFPage(context, pageReference); // Render the PDF page into the context

    CGPDFDocumentRelease(myDocumentRef);
    myDocumentRef = NULL;
}

Here there is a good link where you can find more information about it. The post explains how to use all this methods.

http://iphonedevelopment.blogspot.ch/2008/10/demystifying-cgaffinetransform.html

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