【问题标题】:CAGradientLayer showing as solid colorCAGradientLayer 显示为纯色
【发布时间】:2011-03-27 10:23:16
【问题描述】:

我正在尝试在视图上设置渐变背景,但我下面的代码使 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];

【问题讨论】:

    标签: ios core-animation


    【解决方案1】:

    您的白色和黑色处于灰度色彩空间中。尝试在 RGB 颜色空间中制作它们:

    gradientLayer.colors = @[
        (id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor,
        (id)[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor
    ];
    

    这应该可以解决您的问题,但我不确定这背后的 Quartz 机制。

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多