CGContext line drawing: CGContextFillPath not working? - iphone

Anyone have any idea why CGContextFillPath won't work in the code snippet below? I'm using the following code to draw to a UIImageView. It's stroking the path correctly, but ignoring CGContextFillPath.
UIGraphicsBeginImageContext(self.frame.size);
[drawingView.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0);
CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), firstMovedTouch.x, firstMovedTouch.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFillPath(UIGraphicsGetCurrentContext());
drawingView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

To solve this problem, use the following code:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, 8.0);
CGMutablePathRef pathRef = CGPathCreateMutable();
/* do something with pathRef. For example:*/
CGPathMoveToPoint(pathRef, NULL, x, y);
CGPathAddLineToPoint(pathRef, NULL, x, y+100);
CGPathAddLineToPoint(pathRef, NULL, x+100, y+100);
CGPathAddLineToPoint(pathRef, NULL, x+100, y);
CGPathCloseSubpath(pathRef);
CGContextAddPath(context, pathRef);
CGContextFillPath(context);
CGContextAddPath(context, pathRef);
CGContextStrokePath(context);
CGPathRelease(pathRef);

I believe that the best way to stroke and fill your path is to add the following line of code after you've finished setting up your path:
CGContextDrawPath(myContext, kCGPathFillStroke);

I had the same trouble. I found that you can not use
CGContextStrokePath( UIGraphicsGetCurrentContext() )
CGContextFillPath( UIGraphicsGetCurrentContext() )
consecutively: the latter will not work because after committing the stroke on the path, the path is removed from the context.
So, I used the same path twice; one for strokeļ¼Œthe other for fill.
// ... create a path of CGMutablePathRef ....
CGContextSaveGState( context );
//....fill the path .....
CGContextRestoreGState( context );
CGContextSaveGState ( context );
//....stroke the path .....
CGContextRestoreGState( context );
CFRelease( path );

I think you need to call CGContextClosePath before you can fill the path. I am not sure what you are trying to draw, but CGContextFillPath will fill the area inside the path and I only see single line here.

Related

Creating a Path in multiple steps

I am trying to create a path, then immediately retrieve it and modify it. The problem is that the path gets created and displayed correctly but then I am unable to retrieve it and modify it further.
UPDATE: The problem is that when I call CGContextClosePath in the second function the compiler returns this error:
: CGContextClosePath: no current point.
It basically does not "recognise" the old path and therefore it cannot close it. Any advice would be highly appreciated! Thanks!
Here is my code:
//Defined at the very beginning outside the functions
CGMutablePathRef _path;
//First function
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), BrushSize);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 1.0, 1.0, 1.0);
_path = CGPathCreateMutable();
CGContextAddPath (context, _path);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Second function
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), BrushSize); //1.0); // Brush size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 0.4, 1.0, 1.0);
CGPathRef pathFill = CGPathCreateCopy (_path);
CGContextAddPath (context, pathFill);
CGContextClosePath(context);
CGContextFillPath(context);
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Try adding the path to the context again before you stroke it.
CGContextAddPath (UIGraphicsGetCurrentContext(), _path);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
It that doesn't work, try creating a copy of the original path and add a new line to that instead of trying to use the original. Use:
CGPathCreateCopy

Draw a Line in UIView between 2 image views

I want to draw line between to imageviews (like linking the images..) in UIView. I have used
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.0);
CGContextMoveToPoint(context, 0,0); //start at this point
CGContextAddLineToPoint(context, 20, 20); //draw to this point
CGContextStrokePath(context);
But here, my context is null. I dono whether i have to import any framework or import any headers. Any suggestion or sample will be appreciated.
Thanks in advance.
The very easy way is use label with height 1.:)
UILabel *seperator=[[UILabel alloc]initWithFrame:CGRectMake(0, 53, 233, 1)];
seperator.backgroundColor=[UIColor redColor];
[loginView addSubview:seperator];
UIGraphicsBeginImageContext(image_signature.image.size);
[yourImageView.image drawInRect:CGRectMake(0, 0, yourImageView.image.size.width, yourImageView.image.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 0, 0);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 20, 20);
CGContextStrokePath(UIGraphicsGetCurrentContext());
[yourImageView setImage:UIGraphicsGetImageFromCurrentImageContext()];
UIGraphicsEndImageContext();
...and "yourImageView" is an imageview where you will draw the line. I suggest you to draw the line on each upper imageview, at the very bottom, it's an option.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 5.0);
CGContextMoveToPoint(context, 90, 90.0);
CGContextAddLineToPoint(context, 120.0, 150.0);
CGContextStrokePath(context);

How to get diagram with images as a boarder line in iOS?

I drew rectangle diagram with boarder dotted blue line. I would like to rectangle diagram with same images as a boarder line. That means i want to replace dots with images
My Code:
- (void)drawRect:(CGRect)rect
{
context =UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 1.0, 1.0);
CGFloat dashPattern[10] = {3.0, 2.0};
CGContextSetLineDash(context, 0, dashPattern, 2);
CGContextSetRGBFillColor(context, 0.0, 1.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextAddRect(context, self.bounds);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextClosePath(context);
CGContextStrokePath(context);
}
Can i set image instead of dots? Please give any suggestions to me. Thanks in advance.
I don't try yet, but maybe you can use:
CGContextSetStrokeColorWithColor(context, [UIColor colorWithPatternImage:myImage].CGColor);

Drawing a line with a shadow, but only want to keep shadow. IOS

I am trying to draw a line with a shadow but I do not want to keep the line but only the shadow.
I have attempted to set the stroke color of the line to clear but when I do that the shadow also disappears.
The following code creates 2 lines, i only want to keep the shadow because it looks nicer and its not pixelated like the line.
Is this possible?
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextSetShouldAntialias(UIGraphicsGetCurrentContext(), YES);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(UIGraphicsGetCurrentContext(), CGSizeMake(10,10), 4.0, shadowColor);
Thanks.
So you can't do this directly because the shadow reflects the path, so if you make the path transparent, the shadow will also be transparent. There are a few workarounds I can think of (depends on exactly what you're doing) but one sneaky way would just be to draw the shadow far enough below the path that you can just draw over it, basically erasing it. For example, if the background is white, this will accomplish what you want:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0, 0.0, 0.0, 1.0};
CGColorRef shadowColor = CGColorCreate(colorSpace, components);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f,20.0f), 4.0, shadowColor);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 10.0);
CGContextMoveToPoint(context, 10.0, 30.0);
CGContextAddLineToPoint(context, 310.0, 30.0);
CGContextStrokePath(context);
}
hope this helps.
why dont you just draw your line as a shadow. Dont have the shadow drawn but instead draw a black line with .25 or less alpha and offset it to where you would expect the shadow to be.

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);