使用多层时无法释放未使用的CALayer内存 [英] Can't release unused CALayer memory when using multiple layers

查看:146
本文介绍了使用多层时无法释放未使用的CALayer内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用多个CALayer,并以大型UIImage作为内容. 不幸的是,当我不需要图层或图像时,不会释放内存.

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];

当我使用仪器内存分析器时,我看到创建该层时实际内存增加了,但是释放该层时却没有减少. 由于使用的是ARC,因此无法使用release.我在这里做错了什么?

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 ?

谢谢.

推荐答案

UIImage的imageNamed:方法使用仅在内存不足时释放的静态缓存.

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

您的选择是:

  1. 改为使用-[UIImage imageWithContentsOfFile:].
  2. 忽略该问题.出现内存通知时,将清除缓存.

这篇关于使用多层时无法释放未使用的CALayer内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆