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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:15:43+00:00 2026-05-15T18:15:43+00:00

I have a view in which I paint a dotted line when the user

  • 0

I have a view in which I paint a dotted line when the user moves their finger across the screen of the iPad/iPhone. I draw the line between a point stored as LastLocation, and the point that is the CurrentLocation. The drawings are to be persistant on the screen.
This happens every time the touchMoved event is fired, and ends up letting me draw a dotted line tracing where the person has dragged their finger…. just like a painting application.

I have a function that gets called that does the following when the touch event is fired.
The view contains a UIImageView called drawImage. I use UIImageView as a means of persisting the lines drawn. This clearly isnt how people usually do paint applications, as it is quite slow.
Any insight in to a better way to do persistant painting using the CGContextRef calls would be appreciated.

/* Enter bitmap graphics drawing context */
UIGraphicsBeginImageContext(frame.size);
/* Draw the previous frame back in to the bitmap graphics context */
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];       
/* Draw the new line */
CGContextRef ctx = UIGraphicsGetCurrentContext();
/* Set line draw color to green, rounded cap end and width of 5 */
CGContextSetLineDash(ctx, 0, dashPattern, 2);
CGContextSetStrokeColorWithColor(ctx, color);
CGContextSetLineCap(ctx, kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(ctx, 5.0); // for size
/* Start the new path point */
CGContextMoveToPoint(ctx, LastLocation.x, LastLocation.y);
CGContextAddLineToPoint(ctx, Current.x, Current.y);
CGContextStrokePath(ctx);
/* Push the updated graphics back to the image */
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The ‘drawImage.image drawInRect’ call is extremely slow, and is in essence redrawing the entire image.

Is there a faster way of doing this? I have seen drawing code like this in a few places on blogs for painting, but it’s just a bit slow.
Would love to hear some thoughts on the topic.

  • 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-15T18:15:43+00:00Added an answer on May 15, 2026 at 6:15 pm

    There is no need to composite the image and line manually. Have the view that draws the line above another UIImageView that draws the image. Let the system do the compositing and draw the image.

    In your code, just do the stuff between the two drawImage lines in the drawRect: method of the line drawing view.

    -(void) drawRect:(CGRect)dirty {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        /* Set line draw color to green, rounded cap end and width of 5 */
        CGContextSetLineDash(ctx, 0, dashPattern, 2);
        CGContextSetStrokeColorWithColor(ctx, color);
        CGContextSetLineCap(ctx, kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
        CGContextSetLineWidth(ctx, 5.0); // for size
        /* Start the new path point */
        CGContextMoveToPoint(ctx, LastLocation.x, LastLocation.y);
        CGContextAddLineToPoint(ctx, Current.x, Current.y);
        CGContextStrokePath(ctx);
    }
    

    When one end of the line moves, save the new point and mark the line drawing view as needing to display. So both Current and LastLocation should be members of the line drawing view, and in the setter method for each, call setNeedsDisplay.

    Make sure clearsContextBeforeDrawing is YES and opaque is NO for the line drawing view.

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

Sidebar

Related Questions

I have an ASP.NET MVC view which contains checkboxes for user-defined categories. <td><% foreach
I have a feature which allows the user to create projects and view it
I have a view which contains a form, the form posts and the data
I currently have a list view which has several rows of data and I
I have a list view control which at the moment only allows one item
I have an application, built using MVC, that produces a view which delivers summary
I have a View class (OrderView.aspx) which shows the details of an order (Account
I have a strongly-typed MVC View Control which is responsible for the UI where
I have quite a lot of PHP view files, which I used to include
I have a FrameLayout in which I have 2 controls: - a custom view

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.