渲染PDF iOS-iPad- Quartz的问题

时间:2011-04-08 14:25:40

标签: ios ipad pdf uiscrollview

我将pdf渲染到UIScroolView时遇到问题。当我以横向模式启动应用程序时,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);}

非常感谢!!

1 个答案:

答案 0 :(得分:1)

我已经修复了我的问题,并添加了

theScrollView.contentSize= self.view.bounds.size
在DidRotateFromInterfaceOrientation中