渲染 PDF iOS-iPad- Quartz 时出现问题

发布于 2024-10-30 22:22:49 字数 2881 浏览 0 评论 0原文

我在将 pdf 渲染到 UIScrollView 时遇到问题。当我在横向模式下启动应用程序时,pdf 会填充到视图宽度范围,但不会填充到视图高度范围。 pdf 内容显示在屏幕中,但出现在 UIScroolView 栏上,并且在 pdf 内容下方有一个白色区域。但是当我以纵向模式启动应用程序时,我没有任何问题,因为 pdf 已填充到视图边界(宽度和高度)。

scroolView 具有以下属性:

theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.contentSize = self.view.bounds.size;

渲染 pdf 的方法是这样的:

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{

CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;

@synchronized(self) // Briefly block main thread
{
    drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
    drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));

if (drawPDFPageRef != NULL) // Render the page into the context
{
    NSLog(@"DRAW LAYER: drawPDFPageRef != NULL ");

    CGFloat boundsHeight = self.bounds.size.height;

    if (CGPDFPageGetRotationAngle(drawPDFPageRef) == 0)
    {
        NSLog(@"DRAW LAYER: CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
        CGFloat boundsWidth = self.bounds.size.width;

        CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
        CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
        CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);

        CGFloat effectiveWidth = effectiveRect.size.width;
        CGFloat effectiveHeight = effectiveRect.size.height;

        CGFloat widthScale = (boundsWidth / effectiveWidth);
        CGFloat heightScale = (boundsHeight / effectiveHeight);

        CGFloat scale = (widthScale < heightScale) ? widthScale : heightScale;

        CGFloat x_offset = ((boundsWidth - (effectiveWidth * scale)) / 2.0f);
        CGFloat y_offset = ((boundsHeight - (effectiveHeight * scale)) / 2.0f);

        y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust

        CGFloat x_translate = (x_offset - effectiveRect.origin.x);
        CGFloat y_translate = (y_offset + effectiveRect.origin.y);

        CGContextTranslateCTM(context, x_translate, y_translate);

        CGContextScaleCTM(context, scale, -scale); // Mirror Y
    }
    else // Use CGPDFPageGetDrawingTransform for pages with rotation (AKA kludge)
    {
        NSLog(@"DRAW LAYER ELSE : CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");

        CGContextTranslateCTM(context, 0.0f, boundsHeight);
        CGContextScaleCTM(context, 1.0f, -1.0f);

        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
    }

    CGContextDrawPDFPage(context, drawPDFPageRef);
}

CGPDFPageRelease(drawPDFPageRef); // Cleanup
CGPDFDocumentRelease(drawPDFDocRef);}

非常感谢!

I have a problem with rendering a pdf into a UIScroolView. When I launch my application in landscape mode the pdf is filled to the view width bound, but isn't filled to the view height bound. The pdf content is into the screen but appear the UIScroolView bar and under the pdf content I have a white area. But when I launch my application in portrait mode i don't have any issue, because the pdf is filled to the view bound(width and height).

The scroolView has this properties:

theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.contentSize = self.view.bounds.size;

The method that render the pdf is this:

- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{

CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;

@synchronized(self) // Briefly block main thread
{
    drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
    drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}

CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));

if (drawPDFPageRef != NULL) // Render the page into the context
{
    NSLog(@"DRAW LAYER: drawPDFPageRef != NULL ");

    CGFloat boundsHeight = self.bounds.size.height;

    if (CGPDFPageGetRotationAngle(drawPDFPageRef) == 0)
    {
        NSLog(@"DRAW LAYER: CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
        CGFloat boundsWidth = self.bounds.size.width;

        CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
        CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
        CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);

        CGFloat effectiveWidth = effectiveRect.size.width;
        CGFloat effectiveHeight = effectiveRect.size.height;

        CGFloat widthScale = (boundsWidth / effectiveWidth);
        CGFloat heightScale = (boundsHeight / effectiveHeight);

        CGFloat scale = (widthScale < heightScale) ? widthScale : heightScale;

        CGFloat x_offset = ((boundsWidth - (effectiveWidth * scale)) / 2.0f);
        CGFloat y_offset = ((boundsHeight - (effectiveHeight * scale)) / 2.0f);

        y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust

        CGFloat x_translate = (x_offset - effectiveRect.origin.x);
        CGFloat y_translate = (y_offset + effectiveRect.origin.y);

        CGContextTranslateCTM(context, x_translate, y_translate);

        CGContextScaleCTM(context, scale, -scale); // Mirror Y
    }
    else // Use CGPDFPageGetDrawingTransform for pages with rotation (AKA kludge)
    {
        NSLog(@"DRAW LAYER ELSE : CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");

        CGContextTranslateCTM(context, 0.0f, boundsHeight);
        CGContextScaleCTM(context, 1.0f, -1.0f);

        CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
    }

    CGContextDrawPDFPage(context, drawPDFPageRef);
}

CGPDFPageRelease(drawPDFPageRef); // Cleanup
CGPDFDocumentRelease(drawPDFDocRef);}

Thank you very much!!

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

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

发布评论

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

评论(1

耀眼的星火 2024-11-06 22:22:49

我已经解决了我的问题,只需添加

theScrollView.contentSize= self.view.bounds.size

DidRotateFromInterfaceOrientation

I have fixed my issue symply adding

theScrollView.contentSize= self.view.bounds.size

in DidRotateFromInterfaceOrientation

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