删除 UIImageView

发布于 2024-11-06 06:15:18 字数 182 浏览 2 评论 0原文

所以我想做的是,如果 image1image2 碰撞,我想从屏幕上删除 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 技术交流群。

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

发布评论

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

评论(2

若相惜即相离 2024-11-13 06:15:18

从超级视图中删除它

[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;

浮华 2024-11-13 06:15:18

只需将其从您的超级视图中删除即可:

[image1 removeFromSuperview];

如果您之前已正确管理内存,则此时无需释放它。以下是一些场景:

  1. 您的类拥有对image1的引用(即它不是属性)。因此,当您创建了 image1 并将其添加到您的视图中,您确保自动释放它。因此,视图拥有拥有的引用;当它从该视图中删除时,视图将释放它。

  2. 您的类确实拥有对image1的引用(即它是一个属性)。-dealloc中,您已经根据 Objective-C 内存管理习惯释放了 image1,因此当您从超级视图中删除它时,您仍然不会需要执行内存管理。

Just remove it from your superview:

[image1 removeFromSuperview];

If you've managed your memory correctly heretofore, you won't need to release it at this point. Here are a few scenarios:

  1. Your class does not own a reference to image1 (i.e. it's not a property). So, when you created image1 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.

  2. Your class does own a reference to image1 (i.e. it is a property). In -dealloc, you have released image1 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.

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