Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a white color results in a transparent color

So in my custom view, I'm trying to draw a white/gray gradient using Core Graphics. I have the following code:

UIColor *color1 = [UIColor whiteColor];
UIColor *color2 = [UIColor colorWithRed:209.0/255.0 green:212.0/255.0 blue:217.0/255.0 alpha:1.0];

CFMutableArrayRef colors = CFArrayCreateMutable(NULL, 0, NULL);
CFArrayAppendValue(colors, color1.CGColor);
CFArrayAppendValue(colors, color2.CGColor);

CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[2] = {0.0, 1.0};
CGGradientRef gradient = CGGradientCreateWithColors(colorspace, colors, locations);

CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), gradient, topCenter, bottomCenter, kCGGradientDrawsAfterEndLocation);

I think this code is fairly straightforward and should result in a nice white/gray gradient. But it doesn't; it draws a transparent/gray gradient.

I think it may have something to do with the view's background color, which is [UIColor clearColor]. But I cannot change that, as I need to have some portions of my view to be transparent.

Any ideas?

like image 929
Rits Avatar asked Feb 22 '23 19:02

Rits


1 Answers

UIColor *color1 = [UIColor whiteColor];

The above line likely creates a color in the greyscale colorspace.

Try this instead:

UIColor *color1 = [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:1.f];
like image 132
amattn Avatar answered Mar 04 '23 07:03

amattn