iPhone App - Why do my CGContext circles look like black squares?
What is wrong with this drawRect method in my iPhone app custom UIView class? It's supposed to draw a colored circle, however it draws a black square of proper width and height.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
UIColor *c = [UIColor redColo开发者_运维百科r];
const CGFloat *components = CGColorGetComponents([c CGColor]);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat al = components[3];
CGContextSetRGBFillColor(context, red, green, blue, al);
CGContextFillEllipseInRect(context, CGRectMake(x, y, width, width));
}
Try this:
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, CGRectMake(x, y, width, width));
CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor redColor] CGColor]));
CGContextFillPath(ctx);
}
Hope this works fine.
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0);
CGContextSetFillColorWithColor(context , [UIColor redColor].CGColor;
CGContextFillEllipseInRect(context, CGRectMake(x, y, width, width));
}
精彩评论