Las otras respuestas son correctas de que CGPDFPageGetDrawingTransform no se ampliará, sin embargo, puede escalar el rectángulo para capturar solo el cuadro elegido. Use CGPDFPageGetBoxRect() y calcule la escala tal como sugirió Tommy en la respuesta aceptada, luego use eso para escalar el rectángulo de captura pasado a 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);
}
entonces es fácilmente extendido para manejar paisaje también:
- (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);
}
puede por favor responder esta pregunta, yo también estoy tratando de la misma manera, pero no puedo obtener el ajuste exacto http://stackoverflow.com/questions/3908624/cgcontext-pdf-page-aspect-fit – ajay
Ese es un enlace a esta pregunta; ¿probablemente se refería a http://stackoverflow.com/questions/4321681/how-to-fit-pdf-page-in-entire-view? Echaré un vistazo tan pronto como tenga la oportunidad, aunque estoy tristemente en camino de salir ahora ... – Tommy
Actualización menor, CGPDFGetBoxRect parece ser ahora CGPDFPageGetBoxRect pero increíblemente útil. Este tamaño me ha estado volviendo loco. También puede encontrar incluyendo la línea CGContextConcatCTM (ctx, CGPDFPageGetDrawingTransform (myPageRef, kCGPDFCropBox, layer.bounds, 0, true)); antes de CGContextDrawPDFPage ayuda con cualquier PDF que se rotan 90 degees – sradforth