CALayer 和屏幕外渲染 [英] CALayer and Off-Screen Rendering
问题描述
我有一个分页 UIScrollView
,其 contentSize 足够大,可以容纳许多用于缩放的小型 UIScrollView,viewForZoomingInScrollView
是一个 viewController,它包含一个用于绘制 PDF 的 CALayer页到.这让我可以像 ibooks PDF 阅读器一样浏览 PDF.
I have a Paging UIScrollView
with a contentSize large enough to hold a number of small UIScrollViews for zooming, The viewForZoomingInScrollView
is a viewController that holds a CALayer for drawing a PDF page onto. This allows me to navigate through a PDF much like the ibooks PDF reader.
绘制PDF(Tiled Layers)的代码位于:
The code that draws the PDF (Tiled Layers) is located in:
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
只需在可见屏幕上添加一个页面"即可自动调用此方法.当我更改页面时,在绘制所有图块之前会有一些延迟,即使对象(页面)已经创建.
And simply adding a 'page' to the visible screen calls this method automatically. When I change page there is some delay before all the tiles are drawn, even though the object (page) has already been created.
我想要做的是在用户滚动到下一页之前渲染下一页,从而防止可见的平铺效果.但是,我发现如果图层位于屏幕外,将其添加到滚动视图不会调用 drawLayer
.
What i want to be able to do is render the next page before the user scrolls to it, thus preventing the visible tiling effect. However, i have found that if the layer is located offscreen adding it to the scrollview doesn't call the drawLayer
.
这里有什么想法/常见的陷阱吗?
Any Ideas/common gotchas here?
我试过了:
[viewController.view.layer setNeedsLayout];
[viewController.view.layer setNeedsDisplay];
注意:这在功能上复制 ibook 的事实与整个应用程序的上下文无关.
推荐答案
正如我上面提到的,如果 CALayers 不在屏幕上,则不会渲染.
As i mentioned above, CALayers don't render if they are offscreen.
我最终没有将 PDF 直接绘制到图层上,而是在需要时将 PDF 页面呈现为图像(呈现 1 页加上和减去一个焦点页面)
I ended up not drawing the PDF directly to the layer but instead, rendered the PDF page to an image when i needed (renders 1 page plus and minus one of the focused page)
这里是渲染代码:
-(UIImage *)renderPDFPageToImage:(int)pageNumber//NSOPERATION?
{
//you may not want to permanently (app life) retain doc ref
CGSize size = CGSizeMake(x,y);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, 750);
CGContextScaleCTM(context, 1.0, -1.0);
CGPDFPageRef page; //Move to class member
page = CGPDFDocumentGetPage (myDocumentRef, pageNumber);
CGContextDrawPDFPage (context, page);
UIImage * pdfImage = UIGraphicsGetImageFromCurrentImageContext();//autoreleased
UIGraphicsEndImageContext();
return pdfImage;
}
这篇关于CALayer 和屏幕外渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!