我正在使用代码
在CGContext上显示pdf页面- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, layer.bounds);
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFBleedBox, layer.bounds, 0, true));
CGContextDrawPDFPage(ctx, myPageRef);
}
问题是pdf页面被绘制在页面的中心,在所有四个边上留下边界。有没有办法让页面适合屏幕。
答案 0 :(得分:16)
扩展Tia的答案;内置方法CGPDFPageGetDrawingTransform将缩小但不会向上缩放。如果你想扩展,那么你需要通过比较CGPDFGetBoxRect与你的内容区域的结果来计算你自己的转换。即席打字:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, layer.bounds);
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);
CGRect cropBox = CGPDFGetBoxRect(myPageRef, kCGPDFCropBox);
CGRect targetRect = layer.bounds;
CGFloat xScale = targetRect.size.width / cropBox.size.width;
CGFloat yScale = targetRect.size.height / cropBox.size.height;
CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
CGContextConcatCTM(ctx, CGAffineTransformMakeScale(scaleToApply, scaleToApply));
CGContextDrawPDFPage(ctx, myPageRef);
}
所以:弄清楚你需要多大程度地缩放文档,使其占据视图的整个宽度,占据整个高度多少,然后实际按这两个值的较小值进行缩放。
答案 1 :(得分:2)
如果PDF页面矩形小于CGPDFPageGetDrawingTransform
参数,rect
将不会返回向上扩展转换。
答案 2 :(得分:1)
粗略阅读您的代码建议kCGPDFBleedBox
的使用应替换为其他设置,可能是kCGPDFMediaBox
。有关详细信息,请参阅here。
答案 3 :(得分:0)
其他答案是正确的,CGPDFPageGetDrawingTransform不会向上扩展,但是您可以缩小矩形以仅捕获所选框。使用CGPDFPageGetBoxRect()并按照Tommy在接受的答案中建议的比例计算比例,然后使用它来缩小传递给CGPDFPageGetDrawingTransform()的捕获矩形。
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, layer.bounds);
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGRect box = CGPDFGetBoxRect(myPageRef, kCGPDFBleedBox);
CGFloat xScale = layer.bounds.size.width / cropBox.size.width;
CGFloat yScale = layer.bounds.size.height / cropBox.size.height;
CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
CGRect captureRect = CGRectMake(0, 0,
layer.bounds.size.width/scaleToApply,
layer.bounds.size.height/scaleToApply);
CGAffineTransform drawXfm = CGPDFPageGetDrawingTransform(myPageRef,
kCGPDFBleedBox,
captureRect,
0,
true);
CGContextScaleCTM(ctx, scaleToApply, -scaleToApply);
CGContextConcatCTM(ctx, drawXfm);
CGContextDrawPDFPage(ctx, myPageRef);
}
然后它也很容易扩展到处理景观:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context
{
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctx, layer.bounds);
CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
CGRect box = CGPDFGetBoxRect(myPageRef, kCGPDFBleedBox);
int rotate;
CGFloat xScale, yScale;
if (box.size.width > box.size.height) {
// landscape
rotate = 270;
xScale = layer.bounds.size.height / cropBox.size.width;
yScale = layer.bounds.size.width / cropBox.size.height;
} else {
// portrait
rotate = 0;
xScale = layer.bounds.size.width / cropBox.size.width;
yScale = layer.bounds.size.height / cropBox.size.height;
}
CGFloat scaleToApply = xScale < yScale ? xScale : yScale;
CGRect captureRect = CGRectMake(0, 0,
layer.bounds.size.width/scaleToApply,
layer.bounds.size.height/scaleToApply);
CGAffineTransform drawXfm = CGPDFPageGetDrawingTransform(myPageRef,
kCGPDFBleedBox,
captureRect,
rotate,
true);
CGContextScaleCTM(ctx, scaleToApply, -scaleToApply);
CGContextConcatCTM(ctx, drawXfm);
CGContextDrawPDFPage(ctx, myPageRef);
}
答案 4 :(得分:0)
这是我的解决方案,它也处理旋转。可能有用。
// Create transformation
int rotation = CGPDFPageGetRotationAngle(pdfPage);
CGRect rect = CGPDFPageGetBoxRect(pdfPage, kCGPDFCropBox);
CGRect rotatedRect = rect;
CGRectApplyAffineTransform(rotatedRect, CGAffineTransformMakeRotation(M_PI * rotation / 180.0));
CGFloat scale = MIN(self.bounds.size.width / rotatedRect.size.width, self.bounds.size.height / rotatedRect.size.height);
// Scale
CGContextConcatCTM(context, CGAffineTransformMakeScale(scale, scale));
// Move left bottom cornet to 0, 0
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(rotatedRect.size.width * 0.5, rotatedRect.size.height * 0.5));
// Rotate
CGContextConcatCTM(context, CGAffineTransformMakeRotation(-M_PI * rotation / 180.0));
// Move center into 0, 0
CGContextConcatCTM(context, CGAffineTransformMakeTranslation(-rect.origin.x - rect.size.width * 0.5, -rect.origin.y - rect.size.height * 0.5));
CGContextDrawPDFPage(context, pdfPage);