arvin Arabi
arvin Arabi

Reputation: 253

Deleting a UIImageView

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?

Upvotes: 0

Views: 960

Answers (2)

Felipe Sabino
Felipe Sabino

Reputation: 18215

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;

Upvotes: 3

Jonathan Sterling
Jonathan Sterling

Reputation: 18375

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.

Upvotes: 1

Related Questions