Ani*_*das 7 pdf iphone core-graphics
我正在使用代码在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);
}
Run Code Online (Sandbox Code Playgroud)
问题是pdf页面被绘制在页面的中心,在所有四个边上留下边界.有没有办法让页面适合屏幕.
Tom*_*mmy 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);
}
Run Code Online (Sandbox Code Playgroud)
因此:计算出你需要多大程度地缩放文档,使其占据视图的整个宽度,占据整个高度多少,然后实际按这两个值中的较小者进行缩放.