UIColor - getRed:... - 引发异常

Eug*_*gen 9 ios4 ios

我有以下测试代码:

CGFloat endRed, endGreen, endBlue, endAlpha;

[[UIColor greenColor] getRed:&endRed green:&endGreen blue:&endBlue alpha:&endAlpha];
Run Code Online (Sandbox Code Playgroud)

我在一个UIView类的drawRect方法中调用.此代码失败,但有异常

2011-11-06 02:29:28.671 Chartous[13457:b303] -[UICachedDeviceRGBColor getRed:green:blue:alpha:]: unrecognized selector sent to instance 0x4e7ea10
2011-11-06 02:29:28.673 Chartous[13457:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor getRed:green:blue:alpha:]: unrecognized selector sent to instance 0x4e7ea10'
Run Code Online (Sandbox Code Playgroud)

这有什么不对?

Ody*_*dys 27

尝试另一种方法:

const CGFloat* components = CGColorGetComponents([[UIColor greenColor] CGColor]);
CGFloat red = components[0];
CGFloat green = components[1];
CGFloat blue = components[2];
CGFloat alpha = CGColorGetAlpha([[UIColor greenColor] CGColor]);
Run Code Online (Sandbox Code Playgroud)


jrt*_*ton 4

该方法仅在ios5及以上版本可用。您是否在模拟器或设备上的早期 iOS 版本上运行它?考虑到你问题的标签,我认为是这样。