gpt4 book ai didi

iphone - 重用 subview

转载 作者:行者123 更新时间:2023-12-03 18:06:47 29 4
gpt4 key购买 nike

我有一个 UIImageView 对象,它只是一个纯黑色矩形。这是我用来选择 View 中的按钮的方法。

问题是,我的 View 中有 49 个这样的按钮,并且所有这些按钮都可以同时选择。

我用来向按钮添加 subview 的是:

UIImageView* selectedSquareView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
[selectedSquareView setImage:[UIImage imageNamed:@"SelectedSquare.png"]];
[button addSubview: selectedSquareView];

我希望 selectedSquareView 能够作为其他按钮的 subview 多次重用,但只保留它的一个分配。我不希望为此目的同时创建 49 个 UIImageView。这可能吗?

如果没有,我应该将它们存储在 NSMutableArray 中以便以后轻松删除吗?

问候

-雷蒙德

最佳答案

您将需要 49 个 UIImageView,而您只需要 1 个 UIImage。 UIImageViews 包含每个按钮的位置、大小、is_higlighted 等信息。

话虽这么说,即使您有很多 UIImage,UIImage 也应该对这些事情非常智能,正如 Apple 在其文档中描述的那样:

In low-memory situations, image data may be purged from a UIImage object to free up memory on the system. This purging behavior affects only the image data stored internally by the UIImage object and not the object itself. When you attempt to draw an image whose data has been purged, the image object automatically reloads the data from its original file. This extra load step, however, may incur a small performance penalty.

You should avoid creating UIImage objects that are greater than 1024 x 1024 in size. Besides the large amount of memory such an image would consume, you may run into problems when using the image as a texture in OpenGL ES or when drawing the image to a view or layer. This size restriction does not apply if you are performing code-based manipulations, such as resizing an image larger than 1024 x 1024 pixels by drawing it to a bitmap-backed graphics context. In fact, you may need to resize an image in this manner (or break it into several smaller images) in order to draw it to one of your views.

或者,如果您确实觉得需要在不使用时删除 UIImageView,您可以按照您的建议将它们存储在数组中并在 viewDidDisappear 上释放它们,然后在 viewWillAppear 上重新创建它们。

关于iphone - 重用 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1224785/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com