较小页面尺寸的图片PDF在iOS6中没有自动实现全屏显示
问题描述:
我试图从PDF中捕获图像并在UIScrollView的iPad应用程序中显示它们。当pdf页面大小为1024 x 768(标准4:3宽高比)时,应用程序将图像渲染为全屏(全尺寸)。但是,当PDF页面大小为720 x 540(相同的4:3纵横比,但尺寸较小)时,我看到图像周围出现白色边框。以下是我试图捕获图像的代码片段。较小页面尺寸的图片PDF在iOS6中没有自动实现全屏显示
CGPDFPageRef thePDFPageRef = CGPDFDocumentGetPage(pdfDocRef, pdfIterator);
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB(); // RGB color space
CGBitmapInfo bmi = (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
thumbnailWidth = 1024;
thumbnailHeight = 768;
CGContextRef context = CGBitmapContextCreate(NULL, thumbnailWidth, thumbnailHeight, 8, 0, rgb, bmi);
if (context != NULL) // Must have a valid custom CGBitmap context to draw into
{
CGRect thumbRect = CGRectMake(0.0f, 0.0f, thumbnailWidth, thumbnailHeight); // Target thumb rect
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, thumbRect); // White fill
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(thePDFPageRef, kCGPDFCropBox, thumbRect, 0, true)); // Fit rect
CGContextSetRenderingIntent(context, kCGRenderingIntentDefault);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawPDFPage(context, thePDFPageRef); // Render the PDF page into the custom CGBitmap context
imageRef = CGBitmapContextCreateImage(context); // Create CGImage from custom CGBitmap context
CGContextRelease(context); // Release custom CGBitmap context reference
}
CGColorSpaceRelease(rgb); // Release device RGB color space reference
CGPDFPageRelease(thePDFPageRef);
}
UIImage *image = [UIImage imageWithCGImage:imageRef scale:scale orientation:0];
任何人都可以帮助我如何自动缩放到全屏,即使PDF页面尺寸较小。
答
我发现解决方案如下缩放矩形。希望这有助于一些身体其他..
(1)使用Get PDF页的矩形
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
(2)做缩放矩形,并应用变换单位矩阵
CGAffineTransform trans = CGAffineTransformIdentity;
trans = CGAffineTransformTranslate(trans, 0, pageRect.size.height);
trans = CGAffineTransformScale(trans, 1.0, -1.0);
rect = CGRectApplyAffineTransform(rect, trans);
(3)使用kCGPDFMediaBox而不是kCGPDFCropBox来避免在大小与标准不同时裁剪pdf。
感谢, 西瓦