1

I am using CGrect to draw circle with border but i didn't get sharp border of circle here is my code:

 CGRect rect = CGRectMake(lastPoint1.x - CircleRadius ,lastPoint1.y - CircleRadius,CircleDia,CircleDia);
    CGContextSetFillColorWithColor(ctx, [Util_color getThemeColor].CGColor);
    CGContextFillEllipseInRect(ctx, rect);
    CGContextSetRGBStrokeColor(ctx, 255.0, 255.0, 255.0, 1.0);
    CGContextStrokeEllipseInRect(ctx, rect);
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(ctx, 3.0);
     CGContextSetShouldAntialias(ctx, YES);
    CGContextSetLineCap(cox,kCGLineCapRound);

but i still get blur border ! Is there any way to get sharp border using CGrect?

MiTal Khandhar
  • 275
  • 1
  • 4
  • 15
  • At least use `CGRectIntegral` on the first line. This rounds the coordinates to integer pixels. See http://stackoverflow.com/a/9975374/526547 – Cyrille Feb 01 '17 at 13:28
  • it apply on view i want to apply in CGrect – MiTal Khandhar Feb 02 '17 at 06:03
  • Integral coordinates are exactly the wrong way round. See the illustrated article I posted in my answer for why. – uliwitness Feb 07 '17 at 17:17
  • Possible duplicate of [How to get a 1 pixel line with NSBezierPath?](http://stackoverflow.com/questions/8016618/how-to-get-a-1-pixel-line-with-nsbezierpath) – uliwitness Feb 11 '17 at 15:16

2 Answers2

2

You're drawing between pixels. See here for an explanation and a fix: http://orangejuiceliberationfront.com/are-your-rectangles-blurry-pale-and-have-rounded-corners/

(This is a duplicate of How to get a 1 pixel line with NSBezierPath? for which I wrote this explanation originally)

Community
  • 1
  • 1
uliwitness
  • 8,532
  • 36
  • 58
  • i don't want to use layer because i want circle in UIimage – MiTal Khandhar Feb 02 '17 at 06:02
  • What do you mean by "layer"? That blog post is not about layers, it is about Quartz drawing. – uliwitness Feb 02 '17 at 10:53
  • NSBezierPath use the layer when it add it to UIImageview not in original images and I want on UIimage then whats the solution ? – MiTal Khandhar Feb 03 '17 at 05:39
  • This is the first time you mentioned that you're using layers. Can you put more information about what you're doing into your question? There is a big difference between whether you are drawing a bezier path or whether you're sticking it into a layer for that to draw it. – uliwitness Feb 03 '17 at 11:04
  • I have one UIimage in which i want to draw circle into image and i upload that image to server. But when i draw circle, its edge(border) is not sharp it looks blur with my above code ... – MiTal Khandhar Feb 07 '17 at 05:18
  • 1
    Post your code. There is no UIImage in the code you posted. We can't help if you leave out half the question. – uliwitness Feb 07 '17 at 17:16
0

Guessing that what the OP means by sharp border is one that hasn't had anti-aliasing applied.

CGContextSetShouldAntialias(ctx, NO);
danh
  • 62,181
  • 10
  • 95
  • 136