我在我的CALayers上绘制了带有某些颜色的线性渐变,但是在相同的输入下,颜色随机地以粉红色绘制在屏幕上。
代码如下:
bottomComponents = bottomColor.colorComponents;
topComponents = topColor.colorComponents;
middleComponents = middleColor.colorComponents;
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGGradientRef glossGradient;
CGColorSpaceRef rgbColorspace;
size_t numberOfLocations = 3;
CGFloat locations[3] = {0.0f, MIDDLE_POINT, 1.0f};
CGFloat components[12] =
{
topComponents.red, topComponents.green, topComponents.blue, topComponents.alpha,
middleComponents.red, middleComponents.green, middleComponents.blue, middleComponents.alpha,
bottomComponents.red, bottomComponents.green, bottomComponents.blue, bottomComponents.alpha
};
rgbColorspace = CGColorSpaceCreateDeviceRGB();
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, numberOfLocations);
CGRect currentBounds = self.bounds;
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetHeight(currentBounds));
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCenter, 0);
CGGradientRelease(glossGradient);
CGColorSpaceRelease(rgbColorspace);
其中
colorComponents
仅返回带有颜色分量的结构。当我在日志中输出颜色时,它是正确的颜色,但是当它在屏幕上显示时,无论起始颜色是什么,它都是粉红色。
我做错了什么可能导致随机粉红色出现吗?
粉红色偶尔会完全出现。我将从完全相同的值加载,并且很少,但是可以肯定的是,它将显示为粉红色。从不同的值加载会产生相同的结果。它的发生时间约为1-2%。
最佳答案
有时您会使用UIColor预定义颜色吗? (例如,greenColor)
我发现那些不能生成适当的组件。
利维
关于objective-c - CALayer:线性渐变问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14075027/