Why CGContextSetFillColorWithColor filling wrong color in a rectangle area in ios

1k views Asked by At

In one of application I am using CGContextSetFillColorWithColor to fill a color in a rectangle area. It is working fine and filling proper colors but sometimes it's filling black color. I don't know the reason behind this issue.

Code :

rectangle = CGRectMake(x,y, width, 187/[appDelegate.result count]);

        CGContextAddRect(context, rectangle);
        CGContextStrokePath(context);


        NSMutableParagraphStyle* p = [NSMutableParagraphStyle new];
        p.alignment = NSTextAlignmentCenter;
     //   p.lineBreakMode=NSLineBreakByWordWrapping;

        UIColor *color1 = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0];
        NSLog(@"color is %@",color1.CGColor);

       CGContextSetFillColorWithColor(context,color1.CGColor);


        //CGContextSetFillColor(context, CGColorGetComponents(color1.CGColor));
        NSLog(@"rectangle is %f",rectangle.size.height);
        CGContextFillRect(context, rectangle);

Log message: color is:

<CGColor 0xc216020> [<CGColorSpace 0xc0124b0> (kCGColorSpaceDeviceRGB)] ( 0.933333 0 0 1)
2

There are 2 answers

0
robert On

Are your r,g,b variables set correctly? Try debugging them if the black fill occurs. I do not know if it helps, but I prefer using [color1 setFill];

0
iMark On

You should close your path when you fill Rect your context! Try it with close the path, then d