Frankeex
Frankeex

Reputation: 449

Drawing a circle in iOS

Trying to draw circles (well pinpoints as stars) onto a black background. Searched through Stack but none of the other solutions are working.

Trying to use;

CGContextRef contextRef = UIGraphicsGetCurrentContext();
CGContextFillEllipseInRect(contextRef, CGRectMake(100, 100, 5, 5));
CGContextSetRGBFillColor(contextRef, 0, 0, 0, 1.0);  

The circles are not appearing and the following error message is appearing in the log:

<Error>: CGContextSetRGBFillColor: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

Any help to solve this (even completely different/easier way to achieve my goal) would be greatly appreciated.

Upvotes: 0

Views: 2009

Answers (3)

Ravi
Ravi

Reputation: 2451

try this to get a circle

    UIView * circle = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 10, 10)];
    circle.layer.borderWidth = 2.0f;
    circle.layer.borderColor = [UIColor redColor].CGColor;
    circle.layer.cornerRadius = circle.frame.size.width/2.0;
    circle.layer.masksToBounds = YES;
    [self.view addSubview: circle];

Upvotes: 0

timpone
timpone

Reputation: 19929

I used this in a recent project and worked fine:

-(UIView *)circleWithColor:(UIColor *)color radius:(int)radius {
  UIView *circle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2 * radius, 2 * radius)];
  circle.backgroundColor = color;
  circle.layer.cornerRadius = radius;
  circle.layer.masksToBounds = YES;
  return circle;
}

and then add to current view (for example in a UIViewController subclass):

  UIView *instoreImageDot=[self circleWithColor:[UIColor redColor] radius:4];
  [self.view addSubview:instoreImageDot];

Upvotes: 1

Pete42
Pete42

Reputation: 906

If you are just trying to draw a circle on screen using CAShapeLayer is the simplest method.

 CAShapeLayer *circle = [CAShapeLayer layer];
 circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)].CGPath;
 circle.fillColor = [UIColor blueColor].CGColor;

 [self.view.layer addSublayer:circle];

This code create a CAShapeLayer and then sets the path of it be an oval in a CGRect you define. This one draws a 100x100 circle on the top left side of the screen. Then you add the layer to any other layer. So for a basic implementation just add it the the layer of the UIView you are working with. Hope it helps.

Upvotes: 1

Related Questions