渲染 PDF iOS-iPad- Quartz 时出现问题
我在将 pdf 渲染到 UIScrollView 时遇到问题。当我在横向模式下启动应用程序时,pdf 会填充到视图宽度范围,但不会填充到视图高度范围。 pdf 内容显示在屏幕中,但出现在 UIScroolView 栏上,并且在 pdf 内容下方有一个白色区域。但是当我以纵向模式启动应用程序时,我没有任何问题,因为 pdf 已填充到视图边界(宽度和高度)。
scroolView 具有以下属性:
theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.contentSize = self.view.bounds.size;
渲染 pdf 的方法是这样的:
- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{
CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;
@synchronized(self) // Briefly block main thread
{
drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));
if (drawPDFPageRef != NULL) // Render the page into the context
{
NSLog(@"DRAW LAYER: drawPDFPageRef != NULL ");
CGFloat boundsHeight = self.bounds.size.height;
if (CGPDFPageGetRotationAngle(drawPDFPageRef) == 0)
{
NSLog(@"DRAW LAYER: CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
CGFloat boundsWidth = self.bounds.size.width;
CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
CGFloat effectiveWidth = effectiveRect.size.width;
CGFloat effectiveHeight = effectiveRect.size.height;
CGFloat widthScale = (boundsWidth / effectiveWidth);
CGFloat heightScale = (boundsHeight / effectiveHeight);
CGFloat scale = (widthScale < heightScale) ? widthScale : heightScale;
CGFloat x_offset = ((boundsWidth - (effectiveWidth * scale)) / 2.0f);
CGFloat y_offset = ((boundsHeight - (effectiveHeight * scale)) / 2.0f);
y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust
CGFloat x_translate = (x_offset - effectiveRect.origin.x);
CGFloat y_translate = (y_offset + effectiveRect.origin.y);
CGContextTranslateCTM(context, x_translate, y_translate);
CGContextScaleCTM(context, scale, -scale); // Mirror Y
}
else // Use CGPDFPageGetDrawingTransform for pages with rotation (AKA kludge)
{
NSLog(@"DRAW LAYER ELSE : CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
CGContextTranslateCTM(context, 0.0f, boundsHeight);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
}
CGContextDrawPDFPage(context, drawPDFPageRef);
}
CGPDFPageRelease(drawPDFPageRef); // Cleanup
CGPDFDocumentRelease(drawPDFDocRef);}
非常感谢!
I have a problem with rendering a pdf into a UIScroolView. When I launch my application in landscape mode the pdf is filled to the view width bound, but isn't filled to the view height bound. The pdf content is into the screen but appear the UIScroolView bar and under the pdf content I have a white area. But when I launch my application in portrait mode i don't have any issue, because the pdf is filled to the view bound(width and height).
The scroolView has this properties:
theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.contentSize = self.view.bounds.size;
The method that render the pdf is this:
- (void)drawLayer:(CATiledLayer *)layer inContext:(CGContextRef)context{
CGPDFPageRef drawPDFPageRef = NULL;
CGPDFDocumentRef drawPDFDocRef = NULL;
@synchronized(self) // Briefly block main thread
{
drawPDFDocRef = CGPDFDocumentRetain(_PDFDocRef);
drawPDFPageRef = CGPDFPageRetain(_PDFPageRef);
}
CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f);
CGContextFillRect(context, CGContextGetClipBoundingBox(context));
if (drawPDFPageRef != NULL) // Render the page into the context
{
NSLog(@"DRAW LAYER: drawPDFPageRef != NULL ");
CGFloat boundsHeight = self.bounds.size.height;
if (CGPDFPageGetRotationAngle(drawPDFPageRef) == 0)
{
NSLog(@"DRAW LAYER: CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
CGFloat boundsWidth = self.bounds.size.width;
CGRect cropBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFCropBox);
CGRect mediaBoxRect = CGPDFPageGetBoxRect(drawPDFPageRef, kCGPDFMediaBox);
CGRect effectiveRect = CGRectIntersection(cropBoxRect, mediaBoxRect);
CGFloat effectiveWidth = effectiveRect.size.width;
CGFloat effectiveHeight = effectiveRect.size.height;
CGFloat widthScale = (boundsWidth / effectiveWidth);
CGFloat heightScale = (boundsHeight / effectiveHeight);
CGFloat scale = (widthScale < heightScale) ? widthScale : heightScale;
CGFloat x_offset = ((boundsWidth - (effectiveWidth * scale)) / 2.0f);
CGFloat y_offset = ((boundsHeight - (effectiveHeight * scale)) / 2.0f);
y_offset = (boundsHeight - y_offset); // Co-ordinate system adjust
CGFloat x_translate = (x_offset - effectiveRect.origin.x);
CGFloat y_translate = (y_offset + effectiveRect.origin.y);
CGContextTranslateCTM(context, x_translate, y_translate);
CGContextScaleCTM(context, scale, -scale); // Mirror Y
}
else // Use CGPDFPageGetDrawingTransform for pages with rotation (AKA kludge)
{
NSLog(@"DRAW LAYER ELSE : CGPDFPageGetRotationAngle(drawPDFPageRef) == 0");
CGContextTranslateCTM(context, 0.0f, boundsHeight);
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(drawPDFPageRef, kCGPDFCropBox, self.bounds, 0, true));
}
CGContextDrawPDFPage(context, drawPDFPageRef);
}
CGPDFPageRelease(drawPDFPageRef); // Cleanup
CGPDFDocumentRelease(drawPDFDocRef);}
Thank you very much!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了我的问题,只需添加
DidRotateFromInterfaceOrientation
I have fixed my issue symply adding
in DidRotateFromInterfaceOrientation