(iphone) uiimage、视图、子视图内存管理
我需要在需要时释放 uiimage/view/subviews,并且有一些关于释放它们的正确做法的问题。
[imageViewremoveFromSuperview]会释放imageView和imageView.image?
视图=零;会递归地释放其子视图/和关联的 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.
[imageView removeFromSuperview] would release the imageView and imageView.image?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
removeSuperView 在视图上调用release ,但是你应该注意视图保留计数。仅仅因为您调用了removeFromSuperview并不意味着它正在执行您想要的操作。
不,您可能想要这样做(取决于您在创建子视图期间如何管理子视图。如果超级视图是它们的唯一引用,则它们的保留计数可能为 1,因此在调用removeFromSuperview后调用release将导致错误) :
编辑:要回答您的最后一个问题,不,在视图上调用release不会自动在其所有子视图上调用release。你必须自己做这件事,无论是使用release还是使用removeFromSuperview。
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.
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):
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.
当您执行
[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 inimageView
's dealloc.