gpt4 book ai didi

来自 uicolor 的十六进制颜色

转载 作者:行者123 更新时间:2023-12-03 23:34:16 26 4
gpt4 key购买 nike

我在将 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(例如)十六进制字符串,我们你看到有 7 位数字而不是 6 位,它不是十六进制颜色,任何帮助将不胜感激,谢谢。

最佳答案

NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue];

您将其格式化为带有 %d 的数字

你想用 %x 或 %X 将它格式化为十六进制——也许是字符串 %s 我没有检查函数在做什么以及什么 int hexValue持有

d or i Signed decimal integer 392

x Unsigned hexadecimal integer 7fa

X Unsigned hexadecimal integer (capital letters) 7FA

关于来自 uicolor 的十六进制颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com