Friendlydeveloper
Friendlydeveloper

Reputation: 141

Multiple CALayer performance issue

I'm currently facing some performance issues, when using several custom layers as sublayers inside my view.

After creating my new layer programmatically, I add it as sublayer, like:

[self.view.layer addSublayer:myNewSublayer]; 

This works just fine. However, when doing this over and over my app starts to slow down more and more. I'm pretty sure this happens, because I'm constantly adding a new sublayer to my view. So, in order to solve this problem, I tried to remove everything from self.view.layer before adding new sublayers, like:

[self.view.layer setContents:nil];

Unfortunately this didn't solve my problem.

My new sublayers are correctly released after being added as sublayer. I checked this in instruments and there are definitely no memory leaks. However, I do see that memory is incrementing every time I'm adding new sublayers (which is nothing that would surprise me).

I think I need to find a different way to remove all sublayers from my view's layer.

Any help appreciated. Thanks in advance.

Upvotes: 0

Views: 1005

Answers (1)

Tony Arnold
Tony Arnold

Reputation: 2929

If you're continually adding sublayers to your root layer and not removing them, it's going to get slow & bloaty. You're using the wrong method to remove the sublayers:

[[self.view.layer sublayers] makeObjectsPerformSelector:@selector(removeFromSuperlayer)];

setContents: sets the image contents of the layer (usually via a CGImageRef).

Upvotes: 1

Related Questions