문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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

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];
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];
}

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top