Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear CATiledLayers Cache When Changing Images

I have a UIScrollView with a single subview, a UIView backed by a CATiledLayer. All is working well with one exception: when I change images from one to another the CATiledLayer caches the previous images zoom levels. Scrolling around then displays the old image for a split second before the updated image loads.

Is there any way to totally clear out the CATiledLayer's cache so it doesn't show old images? The CATiledLayer obviously knows that the backing image changed because it asks it's delegate for new tiles...

like image 466
prime31 Avatar asked Aug 13 '09 21:08

prime31


2 Answers

I think you are making this a little more complicated than it needs to be. I believe all you have to do is set the contents of your CATiledLayer to nil.

myCATiledlayer.contents = nil.
like image 117
zpesk Avatar answered Oct 20 '22 00:10

zpesk


-(void)invalidate
{
    CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];

    tiledLayer.tileSize = CGSizeMake(93,93);//Set a different tile size
    tiledLayer.tileSize = CGSizeMake(92,92);//Restore original tile size
}

This works for me.

like image 43
Mikhail Krasnorutsky Avatar answered Oct 20 '22 00:10

Mikhail Krasnorutsky