我有一个名为self.gradientArray
颜色的数组。当我打印出来时它看起来像这样:
gradientsArray: (
"UIDeviceRGBColorSpace 0.333333 0.811765 0.768627 1",
"UIDeviceRGBColorSpace 0.333333 0.811765 0.768627 1" )
我想将此数组转换为CGFloat数组。一个例子是这样的:
CGFloat colors [] = {
1.0, 1.0, 1.0, 1.0,
1.0, 0.0, 0.0, 1.0
};
我试过这个但它没有用。我是Objective-C和语法的新手,所以请帮助我。
CGFloat colors [] = self.gradientArray;
我想使用CGFloat数组制作这样的渐变:
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, colors, NULL, 2);
答案 0 :(得分:2)
gradientsArray
似乎是UIColor
的数组,在这种情况下,您可以执行以下操作:
NSArray *gradientsArray = @[[UIColor redColor], [UIColor blueColor]];
CGFloat colors[gradientsArray.count * 4];
NSInteger index = 0;
for (UIColor *color in gradientsArray) {
CGFloat red, green, blue, alpha;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
colors[index++] = red;
colors[index++] = green;
colors[index++] = blue;
colors[index++] = alpha;
}