Iphone 从UIColor获取RGBA颜色组件的更好方法?

Iphone 从UIColor获取RGBA颜色组件的更好方法?,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,有没有更快(或更好)的方法从UIColor获取[RGBA]颜色组件?这方面似乎有很多变化(这里有一些,基本上都与我在这里所做的相似),我只是好奇是否有一个替代方案,因为它似乎有点“缺乏”,因为其他一切通常都是如此完整和深思熟虑 if([eachKey isEqualToString:@"NSColor"]) { UIColor *newColor = [attrs valueForKey:eachKey]; //NSLog(@"COLOR: %@", newColor);

有没有更快(或更好)的方法从UIColor获取[RGBA]颜色组件?这方面似乎有很多变化(这里有一些,基本上都与我在这里所做的相似),我只是好奇是否有一个替代方案,因为它似乎有点“缺乏”,因为其他一切通常都是如此完整和深思熟虑

if([eachKey isEqualToString:@"NSColor"]) {
    UIColor *newColor = [attrs valueForKey:eachKey];
    //NSLog(@"COLOR: %@", newColor);
    CGColorRef colorRef = [newColor CGColor];
    //NSLog(@"%@", colorRef);
    int numComponets = CGColorGetNumberOfComponents(colorRef);
    if(numComponets == 4) {
        const CGFloat *components = CGColorGetComponents(colorRef);
        CGFloat compR = components[0];
        CGFloat compG = components[1];
        CGFloat compB = components[2];
        CGFloat compA = components[3];

        //NSLog(@"R:%f G:%f B:%f, A:%f", compR, compG, compB, compA);
    }
}
我不是在寻找如何(我想我有上面的长版本)我只是想知道这是你现在应该做的事情吗

CGFloat r, g, b, a;    
[MyColor getRed: &r green:&g blue:&b alpha:&a];
iOS 5+必需。

检查类参考中的方法

- (BOOL)getRed:(CGFloat *)red 
         green:(CGFloat *)green 
          blue:(CGFloat *)blue 
         alpha:(CGFloat *)alpha
上述方法不可靠,对于
[UIColor whiteColor]
(或其他灰度
color
)它将返回NO

使用

if(numComponets==5){

而不是

if(numComponets==4){

从api文档:
这是一个相当容易一点,非常感谢塞瓦。
The size of the array is one more than the number of components of the color 
space for the color.