ioscatiledlayer

iOS CATiledLayer crash


I have a pdf reader app for the iPad where I am using a scrollview to display each page. I keep the page in view and one page either side of the page in view. I have seperate views for portrait and landscape views. The portrait view showns a single page and the landscape viewer shows 2 pages.

When the iPad changes orientation I unload the view for the old orientation and load the view for the new orientation. So say it was in portrait view and then changes to landscape the app unloads the portrait view and loads the landscape view. This all works great except when the pdf's are large.

The pdf's are drawn using tiledlayers. The app is scrashing when the orientation is changed with large pdf's. The app only crashes if the orientation is changed before the tiles have all been drawn. My guess is that it is crashing because it is trying to draw tiles to a view than has been unloaded. So is there a way to stop the drawing of tiles when I unload the view?


Solution

  • You need to set CALayer's delegate to nil, then remove it from the superview. This stops rendering, afterwards you can safely dealloc.

    - (void)stopTiledRenderingAndRemoveFromSuperlayer; {
        ((CATiledLayer *)[self layer]).delegate = nil;    
        [self removeFromSuperview];
        [self.layer removeFromSuperlayer];
    }
    

    Also, make sure to call this from the main thread, or else horrible bugs will await you.