Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 711363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:43:36+00:00 2026-05-14T04:43:36+00:00

I can draw many things using this : NSString *imagePath = [[NSBundle mainBundle] pathForResource:@dummy2.png

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:43:37+00:00Added an answer on May 14, 2026 at 4:43 am

    That’s because setNeedsDisplay does not actually call drawRect:. It simply schedules the view to be redrawn at the next “convenient” time, which is likely the next time, the application re-enters the run-loop. Since you overwrite the remembered command object on each call to the assignment function, by the time, the drawRect: is actually called, only the last assigned command is available and will be drawn.

    A better way to do it would be: remember all commands to be drawn instead just the last one, say in an array, like:

    @interface MyCanvas {
        ...
        NSMutableArray* commandList;
        ...
    }
    

    and add commands to that array instead of assigning a single command member:

    -(void) addCommand:(Command*) cmd {
        [self.commandList addObject: cmd];
    }
    

    The commands should then be processed in your draw method

    for( Command* cmd in self.commandList ) {
    
        [cmd execute ...];
    }
    

    Alternatively, you could define “complex” commands, which consist of more than a single drawing step.

    (EDIT to answer the question in the comments): Your original code did work, because it does the work all in one place in a single invocation of the appropriate draw method. Your last code does not draw anything at all while it runs. It simply remembers (via command object) that something has to be done, and notifies the view, that it should redraw itself on the next convenient occasion. It is important to note, that setNeedsDisplay will not cause any repainting to be done directly. It simply marks the view as “dirty”, which will be picked up by other code in the Cocoa run-time later.

    There is another thing in your code which I find slightly suspicious: your method drawInContext: takes a context argument, which is simply ignored. Neither is it passed on to the execute method of you command object, nor is it installed as some kind of “current” drawing context in an instance variable or somesuch. If you expect the code in drawRect: (or the command’s execute method) to actually use that context, you have to pass it on to whoever is supposed to use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 404k
  • Answers 404k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Layouts are stored in the /layouts/ directory, XSL transformations in… May 15, 2026 at 5:23 am
  • Editorial Team
    Editorial Team added an answer There is an option in Visual Studio that doesn't allow… May 15, 2026 at 5:23 am
  • Editorial Team
    Editorial Team added an answer They are declared in the AssemblyInfo.cs file and embedded in… May 15, 2026 at 5:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.