objective-c - CALayer:线性渐变问题

标签 objective-c ios calayer linear-gradients

我在我的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/

相关文章:

ios - CGAffineTransform 影响我的 UIView 的角半径

ios - AFNetworking POST 错误 3840

iphone - 如何做最新的FB UIView 动画?

ios - Objective C 类找不到 Swift 协议(protocol)的定义

ios - 使用 count() 时 swift 出现奇怪的错误

ios - UIWebView 在资源加载时崩溃的应用程序

objective-c - 如何检测哪个CALayer被点击?

ios5 - UIView.frame 和 CALayer.frame 之间的关系是什么? (在 CATransform3D 之前和之后)

objective-c - 在 Objective-C 中注释 NSArray<NSNumber *> * 以便桥接到 Array<Int>

ios - 相机胶卷图像的创建日期