我在将uicolor转换为十六进制颜色时遇到了问题,这里是我发现的
CGColorRef colorref = [[Colorview_ backgroundColor] CGColor]; int numComponents = CGColorGetNumberOfComponents(colorref); if(numComponents == 4){ const CGFloat * components = CGColorGetComponents(colorref); int hexValue = 0xFF0000 * components [0] + 0xFF00 * components [1] + 0xFF * components [2]; NSString * hexString = [NSString stringWithFormat:@#%d,hexValue]; }这段代码给了我#5576149(例如)hexString,看看有7个数字不是6,它不是一个十六进制颜色,任何帮助将不胜感激,thx。
NSString * hexString = [NSString stringWithFormat:@#%d,hexValue];您将其格式化为包含%d的数字
你想用十六进制格式化为%x或%X - 也许作为字符串%s我没有检查函数正在做什么以及 int hexValue 是持有
d或i有符号十进制整数392x无符号十六进制整数7fa
X无符号十六进制整数(大写字母)7FA
i have a problem in converting uicolor to hex color, here what i found
CGColorRef colorref = [[Colorview_ backgroundColor] CGColor]; int numComponents = CGColorGetNumberOfComponents(colorref); if (numComponents == 4) { const CGFloat *components = CGColorGetComponents(colorref); int hexValue = 0xFF0000*components[0] + 0xFF00*components[1] + 0xFF*components[2]; NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue]; }this code is giving me #5576149 (for example) for hexString, us you see there are 7 digits not 6, it's not a hex color, any help will be appreciated, thx.
解决方案 NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];You are formatting it as a digit with %d
You want to format it as hex with %x or %X -- maybe as string %s I didnt check what the function is doing and what int hexValue is holding
d or i Signed decimal integer 392
x Unsigned hexadecimal integer 7fa
X Unsigned hexadecimal integer (capital letters) 7FA
更多推荐
从uicolor的六角形颜色
发布评论