简体   繁体   English

(iphone)uiimage,view,subviews memory management

[英](iphone) uiimage, view, subviews memory management

I need to release uiimage/view/subviews when I want, and have a few questions regarding proper practice of releasing them. 我需要在需要时发布uiimage / view / subviews,并有一些有关发布它们的正确做法的问题。

  1. [imageView removeFromSuperview] would release the imageView and imageView.image? [imageView removeFromSuperview]会发布imageView和imageView.image吗?

  2. view = nil; 查看=无; would release its subviews/and associated uiimages recursively? 会递归地释放其子视图/相关的uiimage? if not, should I implement a recursive function to release a view's subviews? 如果不是,我应该实现一个递归函数来释放视图的子视图吗?

Thank you 谢谢


Edit. 编辑。

I looked at UIView's library reference 我查看了UIView的库参考

addSubview -- addSubview-

This method retains view and sets its next responder to the receiver, which is its new superview. 此方法保留视图并将其下一个响应者设置为接收器,这是其新的超级视图。

removeFromSuperview -- 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. 仍然不确定[imageView release]是否释放与其关联的uiImage,我是否仍需要递归释放子视图。 ie a view's getting dealloced would automatically guarantee it's subviews release? 即一个视图被取消分配会自动保证它的子视图发布?

When you do [imageView removeFromSuperview] , it won't release anything. 当您执行[imageView removeFromSuperview] ,它不会释放任何内容。 You need to so [imageView release] afterward. 之后,您需要执行[imageView release] Even so, you still need to put your memory releasing for that view in imageView 's dealloc. 即便如此,你仍然需要在imageView的dealloc中释放你的内存。

[imageView removeFromSuperview] would release the imageView and imageView.image? [imageView removeFromSuperview]会发布imageView和imageView.image吗?

removeSuperView calls release on the view , but you should pay attention to the views retain count. removeSuperView在视图上调用release ,但是你应该注意视图保留计数。 Just because you called removeFromSuperview doesn't mean it's doing what you want. 仅仅因为你调用removeFromSuperview并不意味着它正在做你想要的。

view = nil; 查看=无; would release its subviews/and associated uiimages recursively? 会递归地释放其子视图/相关的uiimage? 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): 不,您可能想要这样做(取决于您在创建过程中如何管理子视图。如果超级视图是他们唯一的参考,他们的保留计数可能为1,因此在调用removeFromSuperview后调用release会导致错误) :

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. 编辑:要回答您的最后一个问题,不,在视图上调用release不会自动在其所有子视图上调用release。 You have to do it yourself, whether with release or with removeFromSuperview. 您必须自己完成,无论是发布还是使用removeFromSuperview。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM