iPad问题:在iPad应用程序中使用CATiledLayer显示pdf文件

时间:2011-02-02 10:49:13

标签: ipad

我想使用CATiled Layer在iPad中显示PDF文件 一切都很好但是当我在我的应用程序中使用CATiledLayer并运行它时,PDF文件将显示如图所示Here i saw in iPad the top left corner full of gray color in PDF file displayed in red rounded rectangle

这是我的代码

对于CATiled图层

   tiledLayer = [CATiledLayer layer];
     tiledLayer.delegate = self;
     //Set load block of tiled layer
     tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
     tiledLayer.levelsOfDetail = 1000; 
     tiledLayer.levelsOfDetailBias = 1000;

对于drawInContext

 CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
     CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
     CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
     CGContextScaleCTM(ctx, 1.0, -1.0);
     CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
     CGContextDrawPDFPage(ctx, myPageRef);

请帮我解决这个问题。谢谢你。

1 个答案:

答案 0 :(得分:2)

CGPDFPageGetDrawingTransform不会缩放你的pdf - 你需要编写自己的转换代码来计算pdf的最佳宽高比。

请注意,CGPDFPageGetDrawingTransform还会检查pdf中的 rotation ,您可以像这样用它来手动纠正它:

int rotate = CGPDFPageGetRotationAngle(page);
    switch (rotate) {
        case 0:
            renderingSize.width = roundf(cropBox.size.width * zoom / 100);
            renderingSize.height = roundf(cropBox.size.height * zoom / 100);
            CGContextTranslateCTM(context, 0, cropBox.size.height);
            CGContextScaleCTM(context, 1, -1);
            break;
        case 90:
            renderingSize.width = roundf(cropBox.size.height * zoom / 100);
            renderingSize.height = roundf(cropBox.size.width * zoom / 100);
            CGContextScaleCTM(context, 1, -1);
            CGContextRotateCTM(context, -M_PI / 2.f);
            break;
        case 180:
        case -180:
            renderingSize.width = roundf(cropBox.size.width * zoom / 100);
            renderingSize.height = roundf(cropBox.size.height * zoom / 100);
            CGContextScaleCTM(context, 1, -1);
            CGContextTranslateCTM(context, cropBox.size.width, 0);
            CGContextRotateCTM(context, M_PI * 1.f);
            break;
        case 270:
        case -90:
            renderingSize.width = roundf(cropBox.size.height * zoom / 100);
            renderingSize.height = roundf(cropBox.size.width * zoom / 100);
            CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
            CGContextRotateCTM(context, M_PI / 2.f);
            CGContextScaleCTM(context, -1, 1);
            break;
    }