Why I can't draw in a loop? (Using UIView in iPhone)

Posted by Tattat on Stack Overflow See other posts from Stack Overflow or by Tattat
Published on 2010-04-11T16:54:06Z Indexed on 2010/04/11 17:23 UTC
Read the original article Hit count: 255

I can draw many things using this :

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"dummy2.png" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
image = CGImageRetain(img.CGImage);

CGRect imageRect;

double x = 0;
double y = 0;

for (int k=0; k<someValue; k++) {
    x += k;
            y += k;

        imageRect.origin = CGPointMake(x, y);
        imageRect.size = CGSizeMake(25, 25);    
        CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, image);
    }
}

CGImageRelease(img.CGImage);

So, it works, so, I put it into a command object's execute method. Then, I want to do similar thing, but this time, my execute method only do this:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"dummy2.png" ofType:nil];
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
image = CGImageRetain(img.CGImage);

CGRect imageRect;

double x = inComingX;
double y = inComingY;

 imageRect.origin = CGPointMake(x, y);
        imageRect.size = CGSizeMake(25, 25);    
        CGContextDrawImage(UIGraphicsGetCurrentContext(), imageRect, image);

    CGImageRelease(img.CGImage);

This time, this is also a Command, and it is the execute method. But I take the for loop away. I will have another method that pass the inComingX , and inComingY into my Command object.

My Drawing method is simply execute the Cmd that passed in my drawingEngine:

-(void)drawInContext:(CGContextRef)context
{
    [self.cmdToBeExecuted execute];
}

I also have the assign method to assign the command,:

-(void)assignCmd:(Command* )cmd{
    self.cmdToBeExecuted = cmd;

}

And this is the way I called the drawingEngine

for(int k=0; k<5; k++){
    [self.drawingEngine assignCmd:[DrawingCmd setDrawingInformation:(10*k):0:@"dummy.png"]];
    [self.drawingEngine setNeedsDisplay];
}

It can draw, but the sad thing is it only draw the last one. Why? and how to fix it? I can draw all the things in my First code, but after I take the loop outside, and use the loop in last code, it just only draw the last one. Plz help

© Stack Overflow or respective owner

Related posts about iphone-sdk

Related posts about quartz-graphics