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

  • SEARCH
  • Home
  • 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 8280323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:38:07+00:00 2026-06-08T09:38:07+00:00

I am working on a drawing project, where I have an eraser option. The

  • 0

I am working on a drawing project, where I have an eraser option. The code given below is for when I start my app and draw some lines and the proceed to use the eraser. It works fine and I get the eraser effect. Now the second scenario is where I draw some 10 lines and then click on the “undo button” and undo the whole thing, then I redo the whole thing, and now when I click on the “eraser button” and try to erase some part, but instead, it will clear the whole drawing. This is what I am trying to figure out, but i’m not understanding where I am going wrong so friends, please help me out.

Below is my code.

- (void)drawRect:(CGRect)rect
{


      case DRAW:
    {
        [m_curImage drawAtPoint:CGPointMake(0, 0)];

        CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
        CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
        CGContextRef context = UIGraphicsGetCurrentContext(); 



        [self.layer renderInContext:context];
        CGContextMoveToPoint(context, mid1.x, mid1.y);
        CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
        CGContextSetLineCap(context, kCGLineCapRound);
        CGContextSetLineWidth(context, self.lineWidth);
        CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
        CGContextSetAlpha(context, self.lineAlpha);
        CGContextSetAllowsAntialiasing(context, YES);
        CGContextStrokePath(context); 
       //            [super drawRect:rect];           
    }
         break;



        case ERASE:
        {
            [m_curImage drawAtPoint:CGPointMake(0, 0)];
            CGContextRef context = UIGraphicsGetCurrentContext();     
            CGContextClearRect(context, rect);                
            CGContextSetBlendMode(context, kCGBlendModeClear);
            [super drawRect:rect];
            break;                

  }      


        case UNDO:
        {
            [m_curImage drawInRect:self.bounds];
             break;

        }

        case REDO:
        {
            [m_curImage drawInRect:self.bounds];  
             break;           
        }


        default:
            break;
    }    
}

These are functions that run when I click on undo/redo.

-(void)redrawLine
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    NSDictionary *lineInfo = [m_lineArray lastObject];
    m_curImage = (UIImage*)[lineInfo valueForKey:@"IMAGE"];
    UIGraphicsEndImageContext();
    [self setNeedsDisplayInRect:self.bounds];   
}


-(void)undoButtonClicked
{
    if([m_lineArray count] > 0)
    {
        NSMutableArray *line = [m_lineArray lastObject];
        [m_bufferArray addObject:line];
        [m_lineArray removeLastObject];
        [self redrawLine];
    }
    m_drawStep = UNDO;

}

-(void)redoButtonClicked
{
    if([m_bufferArray count] > 0)
    {
        NSMutableArray *line = [m_bufferArray lastObject];
        [m_lineArray addObject:line];
        [m_bufferArray removeLastObject];        
        [self redrawLine];
    }
     m_drawStep = REDO;

}

Please tell me whether I am doing it right.

Regards,

Ranjit

  • 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-06-08T09:38:09+00:00Added an answer on June 8, 2026 at 9:38 am

    I have solved this issue, the problem and below is the code which works perfectly

    case :ERASE
    {
     CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2); 
                CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
    
                [m_curImage drawAtPoint:CGPointMake(0, 0)];
                CGContextRef context = UIGraphicsGetCurrentContext(); 
    
                [self.layer renderInContext:context];
                CGContextMoveToPoint(context, mid1.x, mid1.y);
                CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y); 
                CGContextSetLineCap(context, kCGLineCapRound);
    
    
                CGContextSetBlendMode(context, kCGBlendModeClear);
                CGContextSetLineJoin(context, kCGLineJoinRound);
                CGContextSetLineWidth(context, self.lineWidth);
                CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
                CGContextSetShouldAntialias(context, YES);  
                CGContextSetAllowsAntialiasing(context, YES); 
                CGContextSetFlatness(context, 0.1f);
    
                CGContextSetAlpha(context, self.lineAlpha);
                CGContextStrokePath(context); 
    }
    

    Regards
    Ranjit.

    This solved my issue, if anyone has the same issue, please implement this.

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

Sidebar

Related Questions

I have the code below that is supposed to draw lines from the top
I am working on a drawing project. My code is working perfectly other than
I am working on a HTML5 Project.There is a drawing graphics API to draw
I am working on a drawing app, with pen touch, on iphone and ipad.
I am working on kind of drawing program but I have a problem with
I'm trying to make a drawing app and currently working on the line tool
I have kind of a unique problem in the project I'm working in. What
I working on a project in which I have to simulate a memory manager
I have started working on a new Xcode project, a game to be exact.
I have been working on XNA for quite some time, but was asked to

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.