我想使用CATiled Layer在iPad中显示PDF文件 一切都很好但是当我在我的应用程序中使用CATiledLayer并运行它时,PDF文件将显示如图所示
这是我的代码
对于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);
请帮我解决这个问题。谢谢你。
答案 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;
}