【问题标题】:CGContext pdf page aspect fitCGContext pdf页面方面适合
【发布时间】:2010-10-11 17:41:10
【问题描述】:

我正在使用代码在 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 页面被绘制在页面的中心,在所有四个边上都留下了边框。有没有办法让页面适合屏幕。

【问题讨论】:

    标签: iphone pdf core-graphics


    【解决方案1】:

    扩展 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);
    }
    

    所以:计算出你需要将文档缩放多少才能占据视图的整个宽度,占据整个高度多少,然后实际缩放这两个值中的较小值。

    【讨论】:

    • 你能回答这个问题吗?我也在尝试同样的方法,但我无法完全匹配stackoverflow.com/questions/3908624/…
    • 这是这个问题的链接;可能你的意思是stackoverflow.com/questions/4321681/…?有机会我会去看看的,虽然我现在很伤心...
    • 小更新,CGPDFGetBoxRect 似乎现在是 CGPDFPageGetBoxRect 但非常有用!这个尺寸让我发疯了。您还可能会发现包括 CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true)); 行在 CGContextDrawPDFPage 帮助任何旋转 90 度的 PDF 之前
    【解决方案2】:

    如果 PDF 页面矩形小于 rect 参数,CGPDFPageGetDrawingTransform 将不会返回放大转换。

    【讨论】:

    • tia,我在其他一些 pdf 阅读器应用程序中尝试了相同的页面,并且显示正确适合屏幕。那么有没有其他方法可以绘制它以使其适合。
    【解决方案3】:

    粗略阅读您的代码表明应将kCGPDFBleedBox 的使用替换为另一个设置,例如kCGPDFMediaBox。请参阅here 了解更多信息。

    【讨论】:

    • fbrereto,我尝试了 kCGPDFMediaBox 和所有其他可用选项。但是显示没有变化。
    【解决方案4】:

    其他答案是正确的,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);
    }
    

    【讨论】:

      【解决方案5】:

      这是我的解决方案,它也处理旋转。可能有用。

      // 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-03
        • 1970-01-01
        • 2018-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多