(iphone) uiimage、视图、子视图内存管理

发布于 2024-10-15 17:43:17 字数 596 浏览 2 评论 0原文

我需要在需要时释放 uiimage/view/subviews,并且有一些关于释放它们的正确做法的问题。

  1. [imageViewremoveFromSuperview]会释放imageView和imageView.image?

  2. 视图=零;会递归地释放其子视图/和关联的 uiimages 吗?如果不是,我应该实现一个递归函数来释放视图的子视图吗?

谢谢


编辑。

我查看了 UIView 的库参考

addSubview --

此方法保留视图并设置其 下一个响应者到接收者, 是它的新超级视图。

从超级视图中删除--

如果接收者的超级视图不是 nil,超级视图释放 接收者。如果您打算重用视图, 致电前请务必保留 这个方法稍后再发布 视情况而定。

仍然不确定 [imageView release] 是否发布了与之关联的 uiImage, 我是否仍然需要递归释放子视图?即视图的释放会自动保证其子视图的释放?

I need to release uiimage/view/subviews when I want, and have a few questions regarding proper practice of releasing them.

  1. [imageView removeFromSuperview] would release the imageView and imageView.image?

  2. view = nil; would release its subviews/and associated uiimages recursively? if not, should I implement a recursive function to release a view's subviews?

Thank you


Edit.

I looked at UIView's library reference

addSubview --

This method retains view and sets its
next responder to the receiver, which
is its new superview.

removeFromSuperview --

If the receiver’s superview is not
nil, the superview releases the
receiver. If you plan to reuse a view,
be sure to retain it before calling
this method and release it again later
as appropriate.

still not sure [imageView release] releases uiImage associated with it,
and would I still need recursive releasing of subviews. ie a view's getting dealloced would automatically guarantee it's subviews release?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-10-22 17:43:17

[imageView removeFromSuperview] 会
释放 imageView 并
imageView.image?

removeSuperView 在视图上调用release ,但是你应该注意视图保留计数。仅仅因为您调用了removeFromSuperview并不意味着它正在执行您想要的操作。

视图=零;将释放其
子视图/和关联的 uiimages
递归地?如果没有,我应该
实现一个递归函数
释放视图的子视图?

不,您可能想要这样做(取决于您在创建子视图期间如何管理子视图。如果超级视图是它们的唯一引用,则它们的保留计数可能为 1,因此在调用removeFromSuperview后调用release将导致错误) :

for (UIView* subview in view){
    [subview removeFromSuperView];
    [subview release]
}
[view release];

编辑:要回答您的最后一个问题,不,在视图上调用release不会自动在其所有子视图上调用release。你必须自己做这件事,无论是使用release还是使用removeFromSuperview。

[imageView removeFromSuperview] would
release the imageView and
imageView.image?

removeSuperView calls release on the view, but you should pay attention to the views retain count. Just because you called removeFromSuperview doesn't mean it's doing what you want.

view = nil; would release its
subviews/and associated uiimages
recursively? if not, should I
implement a recursive function to
release a view's subviews?

No, you probably want to do (depending on how you've managed your subviews during their creation. If the superview was their only reference, they likely have a retain count of 1 and therefore calling release after calling removeFromSuperview will result in an error):

for (UIView* subview in view){
    [subview removeFromSuperView];
    [subview release]
}
[view release];

EDIT: To answer your final question, no, calling release on a view does not automatically call release on all of its subviews. You have to do it yourself, whether with release or with removeFromSuperview.

逆夏时光 2024-10-22 17:43:17

当您执行[imageView removeFromSuperview]时,它不会释放任何内容。之后您需要[imageView release]。即便如此,您仍然需要在 imageView 的 dealloc 中释放该视图的内存。

When you do [imageView removeFromSuperview], it won't release anything. You need to so [imageView release] afterward. Even so, you still need to put your memory releasing for that view in imageView's dealloc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文