其他答案是正確的,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);
}
你可以請回答這個問題我也嘗試相同的方式,但我不能得到確切的適合http://stackoverflow.com/questions/3908624/cgcontext-pdf-page-aspect-fit – ajay 2011-01-14 05:27:57
這是一個鏈接到這個問題;可能你的意思是http://stackoverflow.com/questions/4321681/how-to-fit-pdf-page-in-entire-view?我會盡快看看,只要我有機會,雖然我現在很遺憾地出去...... – Tommy 2011-01-14 13:26:17
微小的更新,CGPDFGetBoxRect似乎現在是CGPDFPageGetBoxRect,但非常有幫助!這個大小讓我瘋狂。你也可以找到包括行CGContextConcatCTM(ctx,CGPDFPageGetDrawingTransform(myPageRef,kCGPDFCropBox,layer.bounds,0,true));在CGContextDrawPDFPage幫助任何旋轉了90張紙張的PDF之前使用 – sradforth 2012-08-23 22:24:49