我在我的一个 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% 的时间里。