CAGradientLayer显示为纯色 [英] CAGradientLayer showing as solid color

查看:119
本文介绍了CAGradientLayer显示为纯色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to set a gradient background on a view, but my code below makes the UIView appear solid black. If i change the whiteColor to greenColor, the gradient is displayed properly. Changing the backgroundColor of the layer to greenColor makes the view appear solid green.

我的猜测是我正在处理某种透明度问题,但我可以'为了解决这个问题。

My guess is that i'm dealing with some kind of transparency-issue, but i can't figure to work around it.

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颜色空间中制作它们:

Your white and black colors are in grayscale colorspace. Try making them in RGB colorspace:

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

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

This should fix your issue, though I'm not sure of the Quartz mechanics behinds this.

这篇关于CAGradientLayer显示为纯色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆