CAGradientLayer显示为纯色

我试图在视图上设置渐变背景,但是我的代码如下使UIView显示为纯黑色。 如果我将whiteColor更改为greenColor,渐变显示正确。 将图层的backgroundColor更改为greenColor使视图显示为纯绿色。

我的猜测是,我正在处理某种透明度问题,但我无法解决这个问题。

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]; 

这应该解决你的问题,但我不确定这背后的石英机制。