设置UIView的背景颜色增加内存使用量 [英] Setting background colour of a UIView increasing memory usage

查看:85
本文介绍了设置UIView的背景颜色增加内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些UIViews加载到UIScrollView上,并试图找出他们使用这么多内存的原因。在使用Instruments消除过程后,我发现设置视图的背景颜色会使内存使用量增加4倍。

I am loading a number of UIViews onto a UIScrollView and am trying to track down why they are using so much memory. After a process of elimination using Instruments I have discovered that setting the background colour of the views increases memory usage by 4 times.

如果我没有设置背景颜色,则内存使用率约为4.5美分。一旦我将背景颜色设置为任何redColor或clearColor,内存使用量就会跳到17megs。

If I don't set the background colour the memory usage sits at around 4.5megs. As soon as I set the background colour to anything redColor or clearColor the memory usage jumps to 17megs.

这是代码:

ThumbnailView *thumbView = [[ThumbnailView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 225.0f, 320.0f)];
thumbView.tag = aCounter;
thumbView.backgroundColor = [UIColor clearColor];

有谁知道这可能导致什么原因?

Does anyone know what could be causing this?

我真正追求的是背景清晰。如果没有办法解决这个问题,是否有另一种方法可以将UIView的背景设置为清楚?

What I am really after is to have the background clear. If there is no way around this, is there another way of setting the background of a UIView to be clear?

推荐答案

所有实例UIView(以及从它派生的类)有一个关联的CALayer对象(通过layer属性访问),它提供了UIView的视觉外观。 CALayer可以拥有自己的位图,它可以与另一个CALayer对象共享一个位图(这就是反射的方式),或者它没有位图。

All instances of UIView (and classes derived from it) have an associated CALayer object (accessed via the layer property) that provides the UIView's visual appearance. The CALayer can have it's own bitmap, it can share a bitmap with another CALayer object (which is how reflections are done), or it can have no bitmap.

当一个UIView充当其他控件的容器,它没有与其图层相关联的位图,因此它使用的内存非常少。一旦设置了它的背景颜色,就必须创建该支持位图,以便有一些东西可以渲染。如果UIView子类实现drawRect在视图中绘制一些图形,那么同样的事情也会发生。

When a UIView acts as a container for other controls, it has no bitmap associated with it's layer, thus it uses very little memory. As soon as you set it's background colour, that backing bitmap has to be created so that there is something to render. If the UIView subclass implements drawRect to draw some graphics into the view, the same thing will happen.

因为全屏大小的视图占用了大量内存,所以当你实现基于UIScrollView的解决方案,您应该只加载显示的视图和两侧的两个。不要提前创建它们。

Because a full-screen sized view consumes a lot of memory, when you implement a UIScrollView based solution, you should only load the views that are displayed and the two either side. Don't create loads of them in advance.

这篇关于设置UIView的背景颜色增加内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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