在网上找了几个方法,都不是很有用在这里分享下一个方法
- (void)getRGBComponents:(CGFloat [3])components forColor:(UIColor *)color {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel,
1,
1,
8,
4,
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipLast);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for (int component = 0; component < 4; component++) {
components[component] = resultingPixel[component] / 255.0f;
}
}
//调用
CGFloat components[4];
[self getRGBComponents:components forColor:color];
NSLog(@"%f %f %f", components[0], components[1], components[2]);