CALayer off-screen rendering

Discussion in 'iOS Programming' started by Luke McNeice, Sep 30, 2010.

  1. Luke McNeice macrumors newbie

    Sep 30, 2010
    First post, Hi all :)

    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.
  2. (marc) macrumors 6502a


    Sep 15, 2010
    the woods
    1) You don't send setNeedsLayout or setNeedsDisplay to CALayers but to UIViews.

    2) What you're trying to do is impossible without some work. iOS will only ever call the drawing code when the content is about to move on-screen. What you can try is to pre-render your view into a UIImage on a separate thread before the user scrolls to it and then put UIImageViews with that image in your UIScrollView. When scrolling, rendering only that UIImage is significantly faster than rendering a complex view hierarchy. When the user is done scrolling, you can remove the UIImageView and show your actual view.
    I hope you understand what I'm saying here, my description isn't very good :)

Share This Page