0

I have a UIColor created by [UIColor colorWithPatternImage:...]; and I want to store it in an NSArray.

I have a problem, however, as I have the following code for normal colors

NSString *NSStringFromUIColor(UIColor *color) {
    //Creates a string with RGB values of the color
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    return [NSString stringWithFormat:@"[%f, %f, %f, %f]",
            components[0],
            components[1],
            components[2],
            components[3]];
}  

But the above crashes with my unconventional patternColorFromImage. So once again, how do I store this color in an NSArray etc?

Thanks

H Bellamy
  • 22,405
  • 23
  • 76
  • 114

5 Answers5

4

NSArray works with Objective-C objects, which the "(float)" cast I see up there most definitely is not.

To be more specific, "float", "int", "char" are C data types, CGFloat is a Core Graphics data structure (and also not an Objective C object).

If you want to save colors in an array, they need to be true "UIColor" Objective-C objects.

If you want to save your CGFloats in a NSArray, you can do the answer in this related question.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

If you want to save parameterized colors in an array, wrap the values in NSNumbers (though, UIColor seems like a better alternative). In other words, either

CGFloat red = 0.5;
NSArray *colorParams = @[ [NSNumber numberWithFloat:red], // etc

or, better:

NSArray *color = [UIColor colorWithRed:red // etc

Either of these objects can be placed in another array:

NSArray *myArrayOfColors = @[ colorParams, color, // but watch out, we have two distinct
// representations of colors in this array
danh
  • 62,181
  • 10
  • 95
  • 136
0

You can't store C types into an NSArray. You could do this:

[NSArray arrayWithObjects: [NSNumber numberWithDouble: components[0]], [NSNumber numberWithDouble: components[1]], nil];

If there in fact are only two components. Here's a more generic version:

CGColorRef cgColor = [myColor CGColor];
NSUInteger numComponents = CGColorGetNumberOfComponents(cgColor);
CGFloat const *components = CGColorGetComponents(cgColor);
NSNumber *componentObjects[numComponents];
for (NSUInteger i = 0; i < numComponents; i++)
    componentObjects[i] = [NSNumber numberWithDouble: components[i]];
NSArray *colorArray = [NSArray arrayWithObjects: componentObjects count: numComponents];
tsnorri
  • 1,966
  • 5
  • 21
  • 29
0
NSString* NSStringFromColor(UIColor* color) {
    CGFloat r, g, b, a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    return [NSString stringWithFormat:@"[%f, %f, %f, %f"], r, g, b, a];
}
apexwill
  • 21
  • 3
-1

In the expression [UIColor redColor], it is obvious to see that redColor is a selector. Also, we can convert selectors to NSStrings and vice-versa with:

NSString colorSel = NSStringFromSelector(@selector(redColor));
SEL redSel = NSSelectorFromString(colorSel");

I'm on a windows PC right now so cannot say for sure whether it should be enclosed in @selector() or not. But you can try them both out to see which one is the right way.

One you get the selector from string, you can set color with:

if ([UIColor respondsToSelector:redSel])
    yourElement.backgroundColor = [UIColor performSelector:redSel];

This is a basic example with basic UIColors. You can use UIColorFromRGB macros, or CGColors too.

Once you get an NSString with your color, you can easily store them in an array.

n00bProgrammer
  • 4,261
  • 3
  • 32
  • 60