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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:44:23+00:00 2026-06-14T19:44:23+00:00

I have this code for drawing on a UIImageView , I want to save

  • 0

I have this code for drawing on a UIImageView, I want to save the drawings into an array and to add a button that removes the last drawing.

How can I do that?

This is my code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    lastPoint = [touch locationInView:self.view];
    lastPoint.y -= 10;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;

    UITouch *touch = [touches anyObject];
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 10;

    UIGraphicsBeginImageContext(self.view.frame.size);
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if(!mouseSwiped) {
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        drawImage.backgroundColor = [UIColor clearColor];
    }
}

Thanks.

  • 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-14T19:44:25+00:00Added an answer on June 14, 2026 at 7:44 pm
           Declare on NSMutableArray object in .h file
             // in .h file
               NSMutableArray *storage;
    
            // in .m file and initialize in view did load
               storage = [[NSMutableArray alloc]init];
    
     - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
                    if ([self.storage count] < MAX_LINE) {
                    if(!mouseSwiped) {
                        UIGraphicsBeginImageContext(self.view.frame.size);
                        [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
                        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
                        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
                        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
                        CGContextStrokePath(UIGraphicsGetCurrentContext());
                        CGContextFlush(UIGraphicsGetCurrentContext());
                        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
                        UIGraphicsEndImageContext();
                        drawImage.backgroundColor = [UIColor clearColor];
                    }
                    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString      stringWithFormat:@"%d_drawslide__.png", [self.storage count]]];
                            [self.storage addObject:path];
                            NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:drawImage.image, path, nil];
                        [NSThread detachNewThreadSelector:@selector(backuppmage:) toTarget:self withObject:params];
                            [params release];
                }
    
     }
    
    -(void) backuppmage:(NSMutableDictionary *) params
      {
            NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
            for (NSString *key in params) {
                  UIImage *image = (UIImage *)[params objectForKey:key];
                  [UIImagePNGRepresentation(image) writeToFile:key atomically:YES];
                  }
             [pool drain];
    
    
      }
    
            // add this method to button click
    -(IBAction) undoEraseDrawing:(id) sender {
    
                if (!isErased && [self.storage count] > 0) {
    
                    [self.storage removeObject:[self.storage lastObject]];
    
                }
    
                if ([self.storage count] > 0 && [self.storage lastObject] != nil) {
                    drawImage.image = [self getImageWithURL:(NSString *)[self.storage lastObject]];
                } else {
                    drawImage.image = nil;
                }
                isErased = NO;
    
        }
    
    
    -(UIImage *) getImageWithURL:(NSString *)url
        {
                return [UIImage imageWithContentsOfFile:url];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have this code in my page, and I want that every NavigateUrl display
I have this line of code: System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); A few lines later,
I have this code for changing the image of a button: - (void)mouseEntered:(NSEvent *)event
I have this code to sort an array of objects. The data in the
I have this code that I call on each touch event that render an
I have some code for drawing polygons edges that is supposed to draw, for
I have this code for drawing my parallax background pGLState.pushModelViewGLMatrix(); final float cameraWidth =
I have this code private void saveImage() { Bitmap bmp1 = new Bitmap(pictureBox.Image); bmp1.Save(c:\\t.jpg,
I have this code into the - (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context method (into a MKOverlayView
I have this code that works fine for regular signed integers that I am

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.