質問

The code below results in a pure red square:

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    CGContextSetLineWidth(context, 5.0f);
    CGContextStrokeRect(context, rect);

}

But the code below results in a white square when what I'd expect is a different shade of red. Can anyone tell me why this is?

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor* boxColour = [UIColor colorWithRed:217 green:37 blue:70 alpha:1.0];
    CGContextSetStrokeColorWithColor(context, boxColour.CGColor);
    CGContextSetLineWidth(context, 5.0f);
    CGContextStrokeRect(context, rect);

}
役に立ちましたか?

解決

The colorWithRed:green:blue:alpha: method of the UIColor class takes parameters in the range 0.0 to 1.0. You probably just need to divide all the values (except alpha) by 255.0f:

UIColor* boxColour = [UIColor colorWithRed:217/255.0f green:37/255.0f blue:70/255.0f alpha:1.0f];

Apple Documentation

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top