CALayer 和离屏渲染

发布于 2024-09-26 05:10:15 字数 703 浏览 7 评论 0原文

我有一个 Paging UIScrollView ,其 contentSize 足够大,可以容纳许多用于缩放的小型 UIScrollView,viewForZoomingInScrollView 是一个 viewController,它容纳一个用于在其上绘制 PDF 页面的 CALayer。这使我能够像 ibooks PDF 阅读器一样浏览 PDF。

绘制 PDF(平铺图层)的代码位于:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;

并且只需将“页面”添加到可见屏幕即可自动调用此方法。当我更改页面时,即使已经创建了对象(页面),在绘制所有图块之前也会有一些延迟。

我想要做的是在用户滚动到下一页之前渲染下一页,从而防止可见的平铺效果。但是,我发现如果图层位于屏幕外,将其添加到滚动视图不会调用drawLayer

这里有什么想法/常见问题吗?

我已经尝试过:

[viewController.view.layer setNeedsLayout]; 
[viewController.view.layer setNeedsDisplay];

注意:在功能上复制 ibook 的事实与完整应用程序的上下文无关。

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.

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.

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?

I have tried:

[viewController.view.layer setNeedsLayout]; 
[viewController.view.layer setNeedsDisplay];

NB: The fact that this is replicating the ibooks functionally is irrelevant within the context of the full app.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2024-10-03 05:10:15

正如我上面提到的,如果 CALayers 位于屏幕外,则它们不会渲染。

我最终没有将 PDF 直接绘制到图层上,而是在需要时将 PDF 页面渲染为图像(渲染 1 页加上和减去焦点页面之一)

以下是渲染代码:

-(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;

}

As i mentioned above, CALayers don't render if they are offscreen.

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)

Here is the render code:

-(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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文