views:

602

answers:

1

Hi. Im having a bit of trouble right now adding the same image multiple times and removing it all at one time after the gamelooop. I manage to add the image by creating 1 UIImageview for every image but i know thats its not the practical way memory wise since im allocating a lot of uiimageview with the same image. I use this code to load the image:

UIImageView *imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"correct.png"]];
UIImageView *imgViewn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"correct.png"]];
CGRect imgRect = CGRectMake((touch.x-20), (touch.y-20), 40, 40);
[imgView1 drawInRect:imgRect];
[imgViewn drawInRect:imgRect];
[imgView1 release]
[imgViewn release]

Basically i use this to add image base on the location of the usertouch made on the subview (with image). If the users set of touches are correct this is where the game loops and show another image for the user to touch. But after the new image load the "correct.png" image which is added on the previous touch is still on view.

Can someone show me a correct way to do this coz im kinda new to programming but i know that im gonna have a memory problem later if i keep on allocating image every time a user touch the screen. Thanks in advance.

A: 

If you're trying to merely move an image (or series of images) through each run of the gameloop, rather than creating and removing them all each time, perhaps create the series of N images only once.

When the touches or gameloop begins, draw the images in the rectangles you're calculating from the touch event, and then when the loop is over, set the hidden property on the object to YES or NO (not sure exactly what your objective is).

It's hard to give you code without more details on what you're doing, but the basic idea is this:

When you first start the program/view, create your N images, and set imageView.hidden = YES;

On each loop, call the [imageView drawInRect:imgRect] method for each image, and set imageView.hidden = NO;.

When the view/program is done, call the [imageView release] method on each object.

wismar
Drahc