views:

46

answers:

2

I've got a TapDetectingImageView class that is pertaining in memory after usage because I do not reuse it in my code. I change my code now to reuse it instead of re creating a new one each time.

The goal is to configure that TapDetectingImageView object and put it in a view. I'll do it 8 times but with the code below, only the last one is showing. I'm sure I'm missing something, but what? ;)

The view holding the imageViews is potatoeView. I'm adding new TapDetectingImageView through:

[potatoeView addSubview:imageView];

What's wrong in that code?

for (int i = 0; i <= numberOfPotatoes-1; i++)
    {
        NSLog(@"potatoesView Generation : %d",i);

        NSArray *imgArray = [[NSArray alloc] initWithArray:[p objectAtIndex:i]];
        UIImage *img1 = (UIImage *) [imgArray objectAtIndex:0];
        UIImage *img2 = (UIImage *) [imgArray objectAtIndex:1];
        NSString *imgName = (NSString *) [imgArray objectAtIndex:2];

        if (imageView == nil) {
            imageView = [[TapDetectingImageView alloc] initWithImage:img1 imageHighlighted:img2];
        }else {

            [imageView setImage:(UIImage *)img1];
            [imageView setImageHighlighted:(UIImage *)img2];

        }

        [imageView setDelegate:self];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"

        CGRect rect = imageView.frame;

        rect.size.height = [potatoesSize floatValue] ;
        rect.size.width = [potatoesSize floatValue] ;

        imageView.frame = rect;

        [imageView setName:(NSString *)imgName];


        imageView.tag = i+1;    // tag our images for later use when we place them in serial fashion

        NSValue *val = [potatoesSizeAndPositions objectAtIndex:i];
        CGPoint point = [val CGPointValue];
        imageView.center = point;

        [potatoeView addSubview:imageView];

        [imgArray release];
    }
+2  A: 

You seem to be fundamentally misunderstanding how your views are drawn. When you set up the same view instance with eight different positions and images, it doesn't "stamp" them all out on some canvas as it goes. Its state won't be queried until the main run loop gets around to drawing your view, at which point it'll ask your view for its frame and contents and so on to draw it. If you want to draw eight different images with UIViews, you'll need eight different views.

zem
+1  A: 

You only ever create one view, set it as the subview and then change it n-1 times. Drop the if (imageView==nil) and create all 8 of them.

John Franklin
Hi there, th'ats what I did before but when dismissing the view, the 8 views are still in memory. I've got not leaks nor zombies but seems to have some abandoned memory generating low memory crash when tested on device ... I'm searching how to fix that ... I'll go back to previous code creating 8 views and releasing afterwards... thanks for the answers.
Tibi
If you're getting a low memory crash, it's not because of eight UIViews. Use the various profiling tools to find the problem instead of taking drastic wild stabs at your application.
zem
ok ooook ;-) I've been back And through instruments anamysis it appears that I'm over releasing things ... have to re analyse my code... sorry ;-)
Tibi
Build with the static analyzer (Cmd-Shift-a). It's good at finding the more common memory leaks.
John Franklin