其他答案是正确的,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) {
rotate = 270;
xScale = layer.bounds.size.height / cropBox.size.width;
yScale = layer.bounds.size.width / cropBox.size.height;
} else {
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);
}