How to stroke the dotted lines using cgcontext iOS? - ios

I have drawn the path using cgpoints but the path is full of dotted lines.I want to stroke the dashed line.
My code:
CGPoint Point1=CGPointMake(184.9489, 383.4082);
CGPoint Point2=CGPointMake(184.9489, 383.4082);
CGPoint Point3=CGPointMake(184.2969, 377.911);
CGPoint Point4=CGPointMake(184.2969, 377.911);
CGPoint Point5=CGPointMake(184.2443, 372.978);
CGPoint Point6=CGPointMake(184.2443, 372.978);
NSArray *arry1=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.x],[NSNumber numberWithFloat:Point3.x],[NSNumber numberWithFloat:Point5.x],nil];
NSArray *arry2=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point1.y],[NSNumber numberWithFloat:Point3.y],[NSNumber numberWithFloat:Point5.y],nil];
NSArray *arry3=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.x],[NSNumber numberWithFloat:Point4.x],[NSNumber numberWithFloat:Point6.x],nil];
NSArray *arry4=[NSArray arrayWithObjects:[NSNumber numberWithFloat:Point2.y],[NSNumber numberWithFloat:Point4.y],[NSNumber numberWithFloat:Point6.y],nil];
for(int i=0;i<[arry2 count];i++)
{
UIGraphicsBeginImageContext(self.view.frame.size);
[img_View.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 3 );
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(),
[UIColor blueColor].CGColor);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), [[arry1 objectAtIndex:i]floatValue], [[arry2 objectAtIndex:i]floatValue]);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), [[arry3 objectAtIndex:i]floatValue], [[arry4 objectAtIndex:i]floatValue]);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeColor);
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
CGContextStrokePath(UIGraphicsGetCurrentContext());
img_View.image = UIGraphicsGetImageFromCurrentImageContext();
[img_View setAlpha:1];
UIGraphicsEndImageContext();}
the o/p is:(vertically 3 dots).
the expected result:combining those three points.
Can anyone please provide me some information regarding this?
Thanks in advance.

The problem is in each loop, the start point and end point of the line is the same point, so it draws as a dot. If you log the point values you will see it. The following code will draw 2 lines.
UIGraphicsBeginImageContext(self.view.frame.size);
[_imageView.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 3.0);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetStrokeColorWithColor(context,
[UIColor greenColor].CGColor);
for(int i=0;i<arry2.count;i++)
{
if (i == 0)
CGContextMoveToPoint(context,[[arry1 objectAtIndex:i]floatValue] ,[[arry2 objectAtIndex:i]floatValue]);
CGContextAddLineToPoint(context, [[arry3 objectAtIndex:i]floatValue], [[arry4 objectAtIndex:i]floatValue]);
}
CGContextSetBlendMode(context,kCGBlendModeColor);
CGContextDrawPath(context, kCGPathFillStroke);
CGContextStrokePath(context);
_imageView.image = UIGraphicsGetImageFromCurrentImageContext();
[_imageView setAlpha:1];
UIGraphicsEndImageContext();

Related

Why The CGContext draw nothing when I fill path color before stroke path color.

Code like this
UIGraphicsBeginImageContextWithOptions(CGSizeMake(100, 100), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 5, 5);
CGContextAddLineToPoint(context, 100, 100);
CGContextSetLineWidth(context, 5);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextFillPath(context);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
when I remove the fill color the image is correct.
But I can't understand why I add fill color the image is nothing?
The problem is the how you draw on the context.
Please try the following:
CGRect rect = CGRectMake(0.0,0.0, 100, 100);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
//Set background color
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,rect);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor grayColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
CGContextMoveToPoint(context, 5, 5);
CGContextAddLineToPoint(context, 100, 100);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Now
_testImage.image = image;
You will get the result. Please let me know your feedback.

How to create circular CGRect

I am using this code to create the rectangle in my app. But i want to make it rounded. How can i do that.
- (void)setCropRect:(CGRect)cropRect
{
if(!CGRectEqualToRect(_cropRect,cropRect)){
_cropRect = CGRectOffset(cropRect, self.frame.origin.x, self.frame.origin.y);
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.f);
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setFill];
UIRectFill(self.bounds);
CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.5].CGColor);
CGContextStrokeRect(context, cropRect);
[[UIColor clearColor] setFill];
UIRectFill(CGRectInset(cropRect, 1, 1));
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
I am using this code to crop the image in rectangular shape:
- (CGImageRef)newTransformedImage:(CGAffineTransform)transform
sourceImage:(CGImageRef)sourceImage
sourceSize:(CGSize)sourceSize
sourceOrientation:(UIImageOrientation)sourceOrientation
outputWidth:(CGFloat)outputWidth
cropRect:(CGRect)cropRect
imageViewSize:(CGSize)imageViewSize
{
CGImageRef source = sourceImage;
CGAffineTransform orientationTransform;
[self transform:&orientationTransform andSize:&imageViewSize forOrientation:sourceOrientation];
CGFloat aspect = cropRect.size.height/cropRect.size.width;
CGSize outputSize = CGSizeMake(outputWidth, outputWidth*aspect);
CGContextRef context = CGBitmapContextCreate(NULL,
outputSize.width,
outputSize.height,
CGImageGetBitsPerComponent(source),
0,
CGImageGetColorSpace(source),
CGImageGetBitmapInfo(source));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, outputSize.width, outputSize.height));
CGAffineTransform uiCoords = CGAffineTransformMakeScale(outputSize.width/cropRect.size.width,
outputSize.height/cropRect.size.height);
uiCoords = CGAffineTransformTranslate(uiCoords, cropRect.size.width/2.0, cropRect.size.height/2.0);
uiCoords = CGAffineTransformScale(uiCoords, 1.0, -1.0);
CGContextConcatCTM(context, uiCoords);
CGContextConcatCTM(context, transform);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextConcatCTM(context, orientationTransform);
CGContextDrawImage(context, CGRectMake(-imageViewSize.width/2.0,
-imageViewSize.height/2.0,
imageViewSize.width,
imageViewSize.height)
,source);
CGImageRef resultRef = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return resultRef;
}
Is i can use the brazier curve to crop the image also.
You cannot create a “circular CGRect”, or a rounded CGRect (which is what you appear to actually want). A CGRect has an origin and a width and a height. It doesn't store any corner radiuses (radii).
Instead of using UIRectFill, you need to create a UIBezierPath of a rounded rect and then fill the path. Example:
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:10];
[[UIColor blackColor] setFill];
[path fill];
You can also stroke a path if you need to. Example:
path = [UIBezierPath bezierPathWithRoundedRect:cropRect cornerRadius:10];
path.lineWidth = 10;
[[[UIColor whiteColor] colorWithAlphaComponent:0.5] setStroke];
[path stroke];

Make a line drawn with CoreGraphics dynamic

I am drawing a line using the following method:
- (void)drawLineWithColor:(UIColor *)color andRect: (CGRect)rect{
if (uploadButtonHidden == 2) {
uploadPhotoButton.hidden = NO;
}
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 15, 110);
// set the stroke color and width
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 6.0);
// move to your first point
CGContextMoveToPoint(context, 455, coords.y - 140);
// add a line to your second point
CGContextAddLineToPoint(context, coordsFinal.x, coordsFinal.y);
// tell the context to draw the stroked line
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Draw image on view
[image drawAtPoint:CGPointZero];
}
As you can see coords.y sets the starting point of my line. Is there any way when changing the coords.y point to update my line? If for example I have a method which adds 50 to coords.y every 0.5 seconds, how can I update the line without redrawing it (to prevent a memory crash)??
EDIT:
this method is called like this:
UIGraphicsBeginImageContext(self.view.frame.size);
[self drawLineWithColor:[UIColor blackColor] andRect:CGRectMake(20, 30, 1476, 1965)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
imageViewForArrows = [[UIImageView alloc] initWithImage:image];
[imageArrowsArray addObject:imageViewForArrows];
Don't draw into an image, draw straight into the context instead. I'm assuming this method is called from within your drawRect method, in which case there will already be a value CGContextRef. You just have to get it and draw into it. Be sure to use CGContextSaveGState and CGContextRestoreGState when you apply a transform or clipping.
- (void)drawLineWithColor:(UIColor *)color andRect: (CGRect)rect{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextTranslateCTM(context, 15, 110);
// set the stroke color and width
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 6.0);
// move to your first point
CGContextMoveToPoint(context, 455, coords.y - 140);
// add a line to your second point
CGContextAddLineToPoint(context, coordsFinal.x, coordsFinal.y);
// tell the context to draw the stroked line
CGContextStrokePath(context);
CGContextRestoreGState(context);
}

drawInRect: losing Quality of Image resolution

I am trying to erase image using following code
CGColorRef strokeColor = [UIColor whiteColor].CGColor;
UIGraphicsBeginImageContext(imgForeground.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[imgForeground.image drawInRect:CGRectMake(0, 0, imgForeground.frame.size.width, imgForeground.frame.size.height)];
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 10);
CGContextSetStrokeColorWithColor(context, strokeColor);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextBeginPath(context);
CGContextMoveToPoint(context, lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(context, currentPoint.x, currentPoint.y);
CGContextStrokePath(context);
imgForeground.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
But I just find out Image losing its resolution due to drawInRect.
You should go with UIGraphicsBeginImageContextWithOptions instead of UIGraphicsBeginImageContext, so that a scale factor can be specified.
For example, this will use the scale factor of the device's main screen:
UIGraphicsBeginImageContextWithOptions(imgForeground.frame.size, NO, 0);

eraser not working in iOS drawing

I am working on a drawing project, where I have an eraser option. The code given below is for when I start my app and draw some lines and the proceed to use the eraser. It works fine and I get the eraser effect. Now the second scenario is where I draw some 10 lines and then click on the "undo button" and undo the whole thing, then I redo the whole thing, and now when I click on the "eraser button" and try to erase some part, but instead, it will clear the whole drawing. This is what I am trying to figure out, but i'm not understanding where I am going wrong so friends, please help me out.
Below is my code.
- (void)drawRect:(CGRect)rect
{
case DRAW:
{
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetAlpha(context, self.lineAlpha);
CGContextSetAllowsAntialiasing(context, YES);
CGContextStrokePath(context);
// [super drawRect:rect];
}
break;
case ERASE:
{
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect);
CGContextSetBlendMode(context, kCGBlendModeClear);
[super drawRect:rect];
break;
}
case UNDO:
{
[m_curImage drawInRect:self.bounds];
break;
}
case REDO:
{
[m_curImage drawInRect:self.bounds];
break;
}
default:
break;
}
}
These are functions that run when I click on undo/redo.
-(void)redrawLine
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0f);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
NSDictionary *lineInfo = [m_lineArray lastObject];
m_curImage = (UIImage*)[lineInfo valueForKey:#"IMAGE"];
UIGraphicsEndImageContext();
[self setNeedsDisplayInRect:self.bounds];
}
-(void)undoButtonClicked
{
if([m_lineArray count] > 0)
{
NSMutableArray *line = [m_lineArray lastObject];
[m_bufferArray addObject:line];
[m_lineArray removeLastObject];
[self redrawLine];
}
m_drawStep = UNDO;
}
-(void)redoButtonClicked
{
if([m_bufferArray count] > 0)
{
NSMutableArray *line = [m_bufferArray lastObject];
[m_lineArray addObject:line];
[m_bufferArray removeLastObject];
[self redrawLine];
}
m_drawStep = REDO;
}
Please tell me whether I am doing it right.
Regards,
Ranjit
I have solved this issue, the problem and below is the code which works perfectly
case :ERASE
{
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();
[self.layer renderInContext:context];
CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetBlendMode(context, kCGBlendModeClear);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetFlatness(context, 0.1f);
CGContextSetAlpha(context, self.lineAlpha);
CGContextStrokePath(context);
}
Regards
Ranjit.
This solved my issue, if anyone has the same issue, please implement this.
Here what is happening is when you erase the whole rect will get clear na?? so instead of using that erase method draw line in same color that of screen so that it will look like window is clearing and its easy too jus try out this.
For example
if your drawing screen is black then draw line for touchesmoved on screen in black color so that it will look like clearing.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
lastPoint = [touch locationInView:imagevw];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:imagevw];
UIGraphicsBeginImageContext(self.imagevw.frame.size);
[self.imagevw.image drawInRect:CGRectMake(0, 0, self.imagevw.frame.size.width, self.imagevw.frame.size.height)];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), width);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0); // black color
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
imagevw.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
This is code to draw line in black color on black view... just try out...it may help you..

Resources