Search code examples
objective-crgbuicolorcgrect

Objective-C drawRect Custom RGB Fill Color


I'm trying to fill a circle in Objective-C with a custom RGB color. Here is my drawRect method:

- (void)drawRect:(CGRect)rect
{
   CGContextRef context = UIGraphicsGetCurrentContext();
   UIColor *color = [UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];

   CGContextSetStrokeColorWithColor(context, [color CGColor]);

   CGContextSetLineWidth(context, 1.0);
   CGContextAddEllipseInRect(context, [ball getRect]);
   CGContextStrokePath(context);
}

Whenever I specify anything in place of [color CGColor] like greenColor it works fine, but with the above code nothing gets rendered (an invisible object). I know it is moving around the page because my update method is NSLog'ing a string for every update.

So, I want to fill and get the outer line of the circle to be the specified RGB color above. What am I doing wrong?

Thanks


Solution

  • [UIColor colorWithRed:green:blue:alpha] accept values between 0.0 to 1.0 (Apple Docs Reference) try changing the code like this:

    UIColor *color = [UIColor colorWithRed:10.0/255.0 green:131.0/255.0 blue:254.0/255.0 alpha:1.0];