Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

objective c - Can't release unused CALayer memory when using multiple layers

I am using multiple CALayer's in my application with large UIImage as contents. unfortunately, when I don't need the layer nor the image - the memory is not freed.

the code I use to create the layer is:

UIImage *im = [UIImage imageNamed:@"image_1.jpg"];
CALayer * l = [CALayer layer];
[l setBounds:CGRectMake(0, 0, 1024, 768)];
[l setPosition:CGPointMake(512, 384)];
[l setAnchorPoint:CGPointMake(0.5, 0.5)];
[l setHidden:NO];
[l setContents:(id) im.CGImage];
[self.layer addSublayer:l]; // self is a subclass of UIView
[self.tmpArr addObject:l]; // self.tmpArr contains the layers I am using (one in this example)

the code I use to release the layer and it's contents is :

CALayer * l = [self.tmpArr objectAtIndex:i];
[l removeFromSuperlayer];
[l setHidden:YES];
[l setContents:nil];
[self.tmpArr removeAllObjects];

when I'm using the instruments memory profiler I see the real memory increasing when creating the layer but never decreasing when freeing there. I can't use release since I am using ARC. what do I do wrong here ?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UIImage's imageNamed: method uses a static cache that is only released upon tight memory situations.

Your options are:

  1. Use -[UIImage imageWithContentsOfFile:] instead.
  2. Ignore the problem. The cache is cleaned up when a memory notification comes in.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...