I am trying out a simple fill, two intersecting circles with same radius and to fill the intersection alone.Below is what I tried
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
// Draw first circle
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextStrokePath(context);
// Draw second circle
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextEOClip(context);
CGContextFillPath(context);
}
My code doesn't even render second circle in context. I have gone through lot of question related to CGContextClip here in forum but most of them uses CGPath in their sample. Only CGpaths can be clipped? Any suggestions or ideas are appreciated.
Thank you
The functions CGContextStrokePath(), CGContextEOClip() and CGContextFillPath()
all clear the current path. Therefore the final CGContextFillPath() has no path to fill.
The following code should now work to draw the intersection of the circles:
// Use first circle as clipping path:
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
// Draw second circle:
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextFillPath(context);
Update: The following code fills and strokes the intersection:
CGContextSaveGState(context);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextRestoreGState(context);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextClip(context);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathStroke);
(Original code which does not work:)
To fill and stroke a path, use CGContextDrawPath():
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 1.0);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextAddArc(context, 150, 150, 50, 0, 2 * M_PI, 1);
CGContextAddArc(context, 200, 150, 50, 0, 2 * M_PI, 1);
CGContextDrawPath(context, kCGPathEOFillStroke);
Did you tried this
- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
and you can also try Flood Fill
Related
This question already has answers here:
How to draw a rounded rectangle in Core Graphics / Quartz 2D?
(9 answers)
Closed 8 years ago.
The code below in my drawRect method draws a rectangle on my view controller. I'd like to set a corner radius to this. What am I missing?
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
Try following, please:
CGRect rectangle = CGRectMake(20, 22, 280, 460);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 255, 255, 255, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
//CGContextFillRect(context, rectangle);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect: rectangle cornerRadius:15.0];
[bezierPath fill];
I want to draw a line with different colors using CGContext.
This is my code
CGSize size = CGSizeMake(200, 200);
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 1, 1);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextAddLineToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextAddLineToPoint(context, 200, 100);
CGContextStrokePath(context);
I am trying this code, its returns error:
<Error>: CGContextAddLineToPoint: no current point.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 1, 1);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context); // and draw blue line
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 200, 100);
CGContextStrokePath(context); // draw red line
Following code maybe help you:
CGSize size = CGSizeMake(200, 200);
//UIGraphicsBeginImageContextWithOptions(size, NO, 0); //comment this line, if you want a image, see bottom two line.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 1, 1); //MoveToPoint First
CGContextAddLineToPoint(context, 50, 50);
CGContextAddLineToPoint(context, 100, 50);
CGContextStrokePath(context);
CGContextBeginPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextMoveToPoint(context, 100, 50); //MoveToPoint First
CGContextAddLineToPoint(context, 200, 100);
CGContextStrokePath(context);
//UIImage* resultImage = UIGraphicsGetImageFromCurrentImageContext(); //if you just need a image
//UIGraphicsEndImageContext(); //match UIGraphicsBeginImageContextWithOptions, if need a image
I am using following context in UIView's extended class's drawrect method to draw Paths and Strings.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);
To Draw Path I use
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1.0f);
CGContextSetLineWidth(context, 1);
CGContextBeginPath(context);
CGContextMoveToPoint(context, origin.x, origin.y);
CGContextAddLineToPoint(context, currentX, origin.y);
......
CGContextStrokePath(context);
To Draw text I use
CGContextSetLineWidth(context, 2.0);
[self.title drawInRect:CGRectMake(100, 100, 200, 40) withFont:font];
I get paths correctly but Text gets up side down! If I remove CGContextScaleCTM and CGContextTranslateCTM I get path up side down! Can someone help me to resolve this please.
Save your previous context before drawing your path and restore it afterward:
CGContextRef context = UIGraphicsGetCurrentContext();
// save the original context
CGContextSaveGState(context);
CGContextScaleCTM(context, 1, -1);
CGContextTranslateCTM(context, 0, -rect.size.height);
// draw path
// restore the context
CGContextRestoreGState();
// draw text
That should do it.
I end up writting following code. May help someone!
- (void)drawText:(NSString*)text context:(CGContextRef)context rect:(CGRect)rect verticle:(BOOL)verticle {
CGAffineTransform translate = CGAffineTransformMakeTranslation(0, -rect.size.height);
CGAffineTransform transform = translate;
if(verticle) {
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI/2);
transform = CGAffineTransformConcat(translate, rotation);
}
CGContextSetTextMatrix(context, transform);
CGContextShowTextAtPoint(context, rect.origin.x, rect.origin.y, [text UTF8String], text.length);
}
How does one draw a crosshairs in an iOS app?
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 0.0, 0.5); // yellow line
CGContextBeginPath(context);
CGContextMoveToPoint(context, 40.0, 40.0); //start point
// Crosshairs go here?!?!?!?
// What do I fill in?
CGContextClosePath(context);
CGContextSetLineWidth(context, 2.0);
CGContextStrokePath(context);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 0.0);
CGContextFillRect(context, rect);
}
See example image below.
This should help you get started in seeing what you are missing. You are very close. Add an ellipse and you'll be done but as others have suggested, a simple quick look at the Quart2d programming guide or any Quart2d tuts will show all of this and more.
Drawing a circle
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 20, 20);
CGContextAddLineToPoint(context, 40, 20);
CGContextStrokePath(context);
CGContextSetStrokeColorWithColor(context, [UIColor yellowColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextBeginPath(context);
CGContextMoveToPoint(context, 30, 10);
CGContextAddLineToPoint(context, 30, 30);
CGContextStrokePath(context);
}
I am drawing a circle in this way:
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
//CGContextSetRGBFillColor(contextRef, 0, 0, 1.0, 1.0);
//CGContextSetRGBStrokeColor(contextRef, 0, 0, 1.0, 1.0);
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]);
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0));
CGContextFillEllipseInRect(contextRef, circlePoint);
I wanted to know how can Make the inside of the circle empty so that I can see what there is behind it.
You mean you only want the outline? Use CGContextStrokeEllipseInRect in that case.
CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(contextRef, 2.0);
CGContextSetStrokeColorWithColor(contextRef, [color CGColor]);
CGRect circlePoint = (CGRectMake(coordsFinal.x, coordsFinal.y, 50.0, 50.0));
CGContextStrokeEllipseInRect(contextRef, circlePoint);
Swift 3
let currentContext = UIGraphicsGetCurrentContext()
currentContext?.setLineWidth(2.0)
currentContext?.setFillColor(color.CGColor)
let circleRect = CGRect(x: coordsFinal.x, y: coordsFinal.y, width: 50.0, height: 50.0)
currentContext?.strokeEllipse(in: circleRect)