Swift - UIGraphics dash line close - swift

This is my code:
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.view.frame.width,graphSize), false, 0)
let context = UIGraphicsGetCurrentContext()
CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextSetLineWidth(context, 0.3)
CGContextBeginPath(context)
CGContextMoveToPoint(context, width1, height1)
CGContextAddLineToPoint(context, width11, height11)
CGContextMoveToPoint(context, width2, height2)
CGContextAddLineToPoint(context, width22, height22)
//dash line
CGContextStrokePath(context)
CGContextMoveToPoint(context, width3, height3)
CGContextAddLineToPoint(context, width33, height33)
CGContextSetLineDash(context, 0, [4], 1)
//next lines...
CGContextMoveToPoint(context, width4, height4)
CGContextAddLineToPoint(context, width44, height44)
I want to have dashes on width3 and height3, but my next lines are also dashed.. How to stop the dash line to be executed ?

You can reset the context like this:
CGContextSetLineDash(context, 0, nil, 0)

Related

How can i draw a semicircle using CoreGraphics

I am trying to draw a semicircle using Core Graphics and Filling it with some color.I want to replace color with image by using CALayer. Can any one help how to do this Thanks!
Code goes here.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 0, 500);
CGContextAddArc(context, 60, 500, 50, 90, 180, 0);
CGContextStrokePath(context);
You need to specify the angles in radians, not degrees. 180 degrees = π radians. So in your example:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 10, 500);
CGContextAddArc(context, 60, 500, 50, M_PI / 2, M_PI, 0);
CGContextStrokePath(context);
Note that I also moved the starting point (…MoveToPoint) 10 pixels to the right, because that’s where the arc begins.
The given answers are not working on iOS 7 for me!
I created a little category to create a circle with the angle of a percentage.
Maybe you wanna check it out.You can still adopt the methode if you only want to create a semicircle.
Here is the: GitHub Link
And that is what its look like:
CGPoint circleCenter = CGPointMake(self.bounds.size.width / 2, self.bounds.size.height / 2);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2);
CGContextMoveToPoint(context, circleCenter.x, circleCenter.y);
CGContextAddArc(context, circleCenter.x, circleCenter.y, 50, 90, 180, 0);
CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y);
CGContextSetFillColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:#"add-icon.png"]].CGColor);
CGContextFillPath(context);
CGContextAddArc(context, circleCenter.x, circleCenter.y, 50, 180, 90, 0);
CGContextAddLineToPoint(context, circleCenter.x, circleCenter.y);
CGContextSetFillColorWithColor(context, [UIColor colorWithPatternImage:[UIImage imageNamed:#"appIcon_58.png"]].CGColor);
CGContextFillPath(context);
What you need, which I found the other day, is a great tool that lets you visualise Core Graphics code - after drawing it in a graphics program.
Its called Paint Code.
PaintCode

Why is my CoreGraphics shape not showing a border?

This is the code I have:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetFillColorWithColor(context, [BUTTON_COLOR CGColor]);
CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextSetLineWidth(context, 1);
int radius = CORNER_RADIUS;
CGContextMoveToPoint(context, 0, self.frame.size.height / 2);
CGContextAddLineToPoint(context, POINT_WIDTH, self.frame.size.height);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius,
rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + radius);
CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + radius,
radius, 0.0f, -M_PI / 2, 1);
CGContextAddLineToPoint(context, POINT_WIDTH, 0);
CGContextAddLineToPoint(context, 0, self.frame.size.height / 2);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);
Why is it not drawing a border around the shape?
EDIT: Now it draws the shape, thanks to changing the last line, but it draws an extra line around it, like this:
http://cl.ly/0u0e051a060m161u0n08
Use CGContextDrawPath(context, kCGPathFillStroke) instead of CGContextFillPath() and CGContextStrokePath().
The thing is, after the first call to CGContextFillPath() or CGContextStrokePath() the context's path is cleared, so the second call does not have a path to work with.

How to stroke a path in Core Graphic?

I'm trying to stroke my path by writing below line of code but its not getting drawn. Please help me to find out whats the problem in this.
void draw1PxStroke(CGContextRef context,CGColorRef color,CGMutablePathRef arcPath) {
CGContextSaveGState(context);
CGContextSetLineCap(context, kCGLineCapSquare);
CGContextSetStrokeColorWithColor(context, color);
CGContextSetLineWidth(context, 3.0);
CGContextAddPath(context, arcPath);
CGContextClip(context);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
The problem is probably your call to CGContextClip which has the side effect of resetting the context's current path to an empty path.
You can do it like this:
CGContextBeginPath(context);
CGContextMoveToPoint(context, rect.origin.x, rect.origin.y);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y);
CGContextAddLineToPoint(context, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y + rect.size.height);
CGContextAddLineToPoint(context, rect.origin.x, rect.origin.y);
CGContextStrokePath(context);
This will display a square

Draw line with special shape like dotted(China Point)

IS it possible to draw line with special shape for example like dotted(China Point) ? i draw line with below code
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, 320, 480)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.867, 0.867, 0.867, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), startPoint.x, startPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), endPoint.x, endPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Maybe CGContextSetLineDash is what you want.
It sets the pattern for dashed lines in a graphics context.
void CGContextSetLineDash (
CGContextRef c,
CGFloat phase,
const CGFloat lengths[],
size_t count
);
This example draws a line with circles (diameter: 20 points, distance: 40 points):
CGContextSetLineWidth(context, 20.0);
CGFloat dash[] = {0.0, 40.0};
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineDash(context, 0.0, dash, 2);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);

Simple drawing with Quartz - Core Graphics

I want to draw a rectangle with the top-left corner rounded with 3 sub-spaces in it. Something like this:
_______
|_|_____|
| |
|_______|
But for some reason I cannot get the inner two lines drawn.
- (void) drawRect:(CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
float cornerRadius = 25.0;
float w = self.bounds.size.width;
float h = self.bounds.size.height;
CGContextMoveToPoint(context, cornerRadius, 0);
CGContextAddQuadCurveToPoint(context, 0, 0, 0, cornerRadius);
CGContextAddLineToPoint(context, 0, h);
CGContextAddLineToPoint(context, w, h);
CGContextAddLineToPoint(context, w, 0);
CGContextAddLineToPoint(context, cornerRadius, 0);
//drawing settings
CGContextSetLineWidth(context, 0.5);
CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor white].CGColor);
//draw rectangle
CGContextDrawPath(context, kCGPathFillStroke);
//draw title/label partition
CGContextMoveToPoint(context, TITLE_HEIGHT, 0);
CGContextAddLineToPoint(context, TITLE_HEIGHT, TITLE_HEIGHT);
CGContextStrokePath(context);
//draw title/content partition
CGContextMoveToPoint(context, 0, TITLE_HEIGHT);
CGContextAddLineToPoint(context, self.bounds.size.width, TITLE_HEIGHT);
CGContextStrokePath(context);
}
I wonder what am I mistaking here... ;(
Thanks in advance
Try to comment out the CGContextSetFillColorWithColor line for a while - maybe the rectangle is overlapping those lines?