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 8138451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:29:56+00:00 2026-06-06T11:29:56+00:00

I have created a custom UIView (without .xib) for a finger paint application. Paint

  • 0

I have created a custom UIView (without .xib) for a finger paint application.

Paint is working fine with custom UIView but my problem is that when I try to erase the painted path I am getting:

Error : Invalid context

Below is my class:

.h file

@interface draw2D : UIView
{
    CGPoint previousPoint;
    CGPoint lastPoint;
    CGMutablePathRef path;
    UIButton *btnClose;
    UIButton *btnErase;
    BOOL IsErase;
}
- (IBAction)btnClose:(id)sender;
- (IBAction)btnErase:(id)sender;
@end


@implementation draw2D
- (void)awakeFromNib
{
    path = CGPathCreateMutable();
}
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        btnClose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnClose addTarget:self action:@selector(btnClose:)
         forControlEvents:UIControlEventTouchDown];
        [btnClose setTitle:@"close" forState:UIControlStateNormal];
        btnClose.frame = CGRectMake(10, 10, 100, 40.0);

        btnErase = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btnErase addTarget:self action:@selector(btnErase:)
           forControlEvents:UIControlEventTouchDown];
        [btnErase setTitle:@"Erase" forState:UIControlStateNormal];
        btnErase.frame = CGRectMake(150, 10, 100, 40.0);

        [self addSubview:btnClose];
        [self addSubview:btnErase];
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{    
    UITouch *touch = [touches anyObject];
    NSLog(@"Touch Began :%d",[touch tapCount]);

    if ([touch tapCount] > 1) 
    {
        NSLog(@"::::: Paint Start :::::");
        path = CGPathCreateMutable();
        previousPoint = lastPoint;
        [self setNeedsDisplay];
    }
    self.backgroundColor = [UIColor clearColor];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    NSLog(@"::::: touchesMoved :::::");
    lastPoint = [[touches anyObject] locationInView:self];
    previousPoint = [[touches anyObject] previousLocationInView:self];

    if(IsErase)
    {
        NSLog(@"erase");
        UITouch *erasetouch = [touches anyObject];  
        CGPoint erasecurrentPoint = [erasetouch locationInView:self];

        CGContextRef erasecontext = UIGraphicsGetCurrentContext();
        CGContextSetLineCap(erasecontext, kCGLineCapRound);
        CGContextSetLineWidth(erasecontext,10);
        CGContextSetBlendMode(erasecontext, kCGBlendModeClear);
        CGContextSetStrokeColorWithColor(erasecontext, [[UIColor clearColor] CGColor]);
        CGContextBeginPath(erasecontext);
        CGContextMoveToPoint(erasecontext, lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(erasecontext, erasecurrentPoint.x, erasecurrentPoint.y);
        CGContextStrokePath(erasecontext);     
        CGContextFlush(erasecontext);

    }  

    [self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
    NSLog(@"::::: drawRect :::::");

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGPathMoveToPoint(path, NULL, previousPoint.x, previousPoint.y);
    CGPathAddLineToPoint(path, NULL, lastPoint.x, lastPoint.y);
    CGContextAddPath(context, path);
    CGContextSetLineWidth(context, 5);
    [[UIColor blueColor] setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
}

- (IBAction)btnClose:(id)sender
{
    [self removeFromSuperview];
}
- (IBAction)btnErase:(id)sender
{
    IsErase = YES;
}    

@end

I have set erase button with functionality but not working.

  • 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-06T11:29:58+00:00Added an answer on June 6, 2026 at 11:29 am

    finally i have find the solution . my mistake was i m implement erase code intouchMove method with new context . i dont have to need new context . implement erase code in drawrect method and now its working fine . see the below code.

    - (void)drawRect:(CGRect)rect
    {
    
        [curImage drawAtPoint:CGPointMake(0, 0)];
        CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
        CGPoint mid2 = midPoint(currentPoint, previousPoint1);
    
        context = UIGraphicsGetCurrentContext(); 
        [self.layer renderInContext:context];
    
        if(IsErase)
        {
            CGContextSetLineWidth(context,self.lineWidth);
            CGContextSetBlendMode(context, kCGBlendModeClear);
            CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
            CGContextBeginPath(context);
            CGContextMoveToPoint(context, mid1.x, mid1.y);
            CGContextAddLineToPoint(context, previousPoint1.x, previousPoint1.y);
            CGContextStrokePath(context);     
            CGContextFlush(context);
        }
        else
        {
            CGContextMoveToPoint(context, mid1.x, mid1.y);
            CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
            CGContextSetLineCap(context, kCGLineCapRound);
            CGContextSetLineWidth(context, self.lineWidth);
            CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
            CGContextSaveGState(context);
            CGContextStrokePath(context);
        }
        [super drawRect:rect];
        [curImage release];
    }
    

    i hope it will helps to someone in erase functionality .

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

Sidebar

Related Questions

I have created a custom subclass of UIView for an interface element that I
I have created a custom view hierarchy in an XIB that I want to
I have created a custom UIView that I would like to use on multiple
I have created a custom UIView that has some set of UILabels in it.
I have created a custom workflow activity that copies attachments from a case to
I have created a custom tag that looks like this: def textField = {
I have created a UIScrollView (canvas.scrollview) and have a custom UIView (canvas) inside it.
I'm developing an iPhone application. I have a class that inherits from UIView .
Setup: I have a custom class I've created that handles data retrieved from a
I have subclassed UIView and created a NIB that controls the main logic for

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.