0

I was researching how to make a circle image by code in objective-c these two days. I found several way to do this, but no matter which way, the image created is not an exact circle, which is cut. Please see following code and image:

CGRect rect = CGRectMake(0.0f, 0.0f, radius*2.0f, radius*2.0f);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);     
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillEllipseInRect(context, rect);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

enter image description here

If you look at the image carefully, you will find that the edge has been cut.

Finally I found if I change the code as following:

CGRect rect = CGRectMake(0.0f, 0.0f, radius*2.0f+4, radius*2.0f+4);
CGRect rectmin = CGRectMake(2.0f, 2.0f, radius*2, radius*2);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillEllipseInRect(context, rectmin);
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;

The result is much better, but I don't think it's a nice solution. Does anybody know exactly what's the problem of my first code snippet? Thanks in advance.

p.s. all the screenshots are captured from the simulator.

enter image description here

Vigor
  • 1,706
  • 3
  • 26
  • 47
  • boy, I stared at those circles for a while. Different colors for circle and bg will help like black and white. Just a FYI – Sam B Aug 08 '15 at 14:06
  • Have a look here: [iOS draw filled Circles][1] [1]: http://stackoverflow.com/questions/17038017/ios-draw-filled-circles – dym Aug 08 '15 at 14:40

0 Answers0