CAGradientLayer显示为纯色
问题描述:
我想在视图上设置渐变背景,但我的代码如下使UIView显示为纯黑色。如果我将whiteColor更改为greenColor,渐变显示正确。将图层的backgroundColor更改为greenColor使视图显示为纯绿色。CAGradientLayer显示为纯色
我的猜测是我正在处理某种透明度问题,但我无法解决这个问题。
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor blackColor] CGColor], nil];
gradientLayer.frame = CGRectMake(0, 0, 1, someView.frame.size.height);
UIGraphicsBeginImageContext(CGSizeMake(1, someView.frame.size.height));
[gradientLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
someView.backgroundColor = [UIColor colorWithPatternImage: image];
答
你的白色和黑色的颜色在灰度色彩空间。尽量使他们在RGB色彩:
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor,
(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor, nil];
这应该可以解决您的问题,但我不知道该石英力学的屁股这一点。