无法将UIColor十六进制字符串值转换为哈希值

时间:2016-02-19 11:57:58

标签: hash hex uicolor

在我的项目中,我想将UIcolour十六进制字符串值转换为哈希值..

当我执行NSLOG功能以打印颜色值时。我得到的值如" UIDeviceRGBColorSpace 0.866667 0.886275 0.415686 1" 。我想将其转换为哈希值。提前致谢

我正在使用此函数将十六进制整数值转换为哈希值

//从十六进制值

获取UIColor
- (UIColor *)colorWithHexString:(NSString *)hexString {
unsigned rgbValue = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&rgbValue];

return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16)/255.0 green:((rgbValue & 0xFF00) >> 8)/255.0 blue:(rgbValue & 0xFF)/255.0 alpha:1.0];

}

1 个答案:

答案 0 :(得分:1)

- (NSString *)hexStringWithColor:(UIColor *)color {
    CGFloat red, green, blue, alpha;
    if ([color getRed:&red green:&green blue:&blue alpha:&alpha]) {
        return [NSString stringWithFormat:@"#%02X%02X%02X",
                                          round(255 * red),
                                          round(255 * green),
                                          round(255 * blue)];
    }
    // Could not get RGB values for color
    return nil;
}