删除 UIImageView
所以我想做的是,如果 image1
与 image2
碰撞,我想从屏幕上删除 image1
(不仅仅是隐藏它,而是删除它)以确保应用程序不会崩溃或使用大量内存的方式。
我认为这与 release
有关,但我不确定。请问我该怎么做?
So what I want to do is if image1
collides with image2
, I want to remove image1
from the screen (not just hide it but remove it) in a way that the application does not crash or use to much memory.
I think it's something related with release
but I'm not sure. How can I do this, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从超级视图中删除它
[image1 removeFromSuperview];
编辑:
如果您有一个指向 image1 的指针,您可能刚刚将其添加到超级视图中并且尚未释放它。因此,如果是这种情况,为了避免任何泄漏,只需在从超级视图中删除它时释放它即可。
[image1removeFromSuperview];
[image1 发布], image1 = nil;
remove it from the superview
[image1 removeFromSuperview];
EDIT:
if you have a pointer to image1, you might have just added it to the superview and did not release it yet. So, if that is the case and to avoid any leaks, just release it when removing it from superview.
[image1 removeFromSuperview];
[image1 release], image1 = nil;
只需将其从您的超级视图中删除即可:
如果您之前已正确管理内存,则此时无需释放它。以下是一些场景:
您的类不拥有对
image1
的引用(即它不是属性)。因此,当您创建了image1
并将其添加到您的视图中,您确保自动释放它。因此,视图拥有拥有的引用;当它从该视图中删除时,视图将释放它。您的类确实拥有对
image1
的引用(即它是一个属性)。在-dealloc
中,您已经根据 Objective-C 内存管理习惯释放了image1
,因此当您从超级视图中删除它时,您仍然不会需要执行内存管理。Just remove it from your superview:
If you've managed your memory correctly heretofore, you won't need to release it at this point. Here are a few scenarios:
Your class does not own a reference to
image1
(i.e. it's not a property). So, when you createdimage1
and added it to your view, you made sure to autorelease it. As such, the view holds the owning reference; when it is removed from that view, the view will release it.Your class does own a reference to
image1
(i.e. it is a property). In-dealloc
, you have releasedimage1
according to the Objective-C memory management idiom, so when you remove it from the superview, you still don't need to perform memory management.