IPhone iOS how to serialize / save UIColor to JSON file?

When I need to save color in the main data, I just use NSKeyedArchiever and then unarchieve color when I need to load an object. However, when I try to serialize the main data object, the process crashes because it does not know how to convert the NSIata key with the keys to a string.

What is the best way to convert UIColor to a string representation for sending in a JSON file? One solution is to store the values ​​of r, g, b, a as a float. One solution is to save it as a hexadecimal string. Both of them seem to complicate the color recovery process.

Am I missing something? Is there an easier way to serialize UIColor to JSON?

+3
source share
5 answers

How about this:

NSDictionary *colorData = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:100.f], @"Red",
                               [NSNumber numberWithFloat:24.f], @"Green",
                               [NSNumber numberWithFloat:23.f], @"Blue",
                               [NSNumber numberWithFloat:1.f], @"Alpha", nil];

NSDictionary *color = [NSDictionary dictionaryWithObject:colorData forKey:@"Color"];

The result looks something like this:

{"Color":{"Green":24,"Alpha":1,"Blue":23,"Red":100}}

You can easily make a method that takes UIColoras an argument and returns an NSDictionary, which can be serialized directly. The reverse process is the same.

+5
source

Since there is no specific standard for JSON for placement UIColor(not to mention a common “color”), you will have to collapse your own.

I would suggest that the best method would be to have a category on UIColor, which gives you the standard hex code for a color in the RGB color space, and also accepts such a hex code and spits out s UIColor, then pass that hex code to JSON.

, , , , .

hex to UIColor MJGFoundation UIColor. UIColor , , RGBA.

+2

"private" . RGBA ( get... BOOL - ), , .

@implementation UIColor (JSON)

- (NSString *)json_stringValue
{
    CGFloat r, g, b, a, h, s, w;
    if ([self getRed:&r green:&g blue:&b alpha:&a]) 
        return [NSString stringWithFormat:@"rgba:%f,%f,%f,%f", r,g,b,a];
    else if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
        return [NSString stringWithFormat:@"hsba:%f,%f,%f,%f", h,s,b,a];
    else if ([self getWhite:&w alpha:&a]) 
        return [NSString stringWithFormat:@"wa:%f,%f", w, a];

    NSLog(@"WARNING: unable to serialize color %@", self);
    return nil;
}

@end

@implementation NSString (JSON)

- (UIColor *)json_color
{
    NSArray *comps = [self componentsSeparatedByString:@":"];
    NSArray *colors = [comps[1] componentsSeparatedByString:@","];
    NSUInteger count = colors.count;
    CGFloat values[4] = {0,0,0,0};
    for (NSUInteger i = 0; i < count; i++) values[i] = [colors[i] floatValue];

    if ([comps[0] isEqualToString:@"rgba"]) 
        return [UIColor colorWithRed:values[0] green:values[1] blue:values[2] alpha:values[3]];
    else if ([comps[0] isEqualToString:@"hsba"])
        return [UIColor colorWithHue:values[0] saturation:values[1] brightness:values[2] alpha:values[3]];
    else if ([comps[0] isEqualToString:@"wa"])
        return [UIColor colorWithWhite:values[0] alpha:values[1]];

    NSLog(@"WARNING: unable to deserialize color %@", self);
    return nil;
}

@end
+1

:

:

#import "UIColor-HSVAdditions.h"
#import "JSONKit.h"

UIColor* color = textView.textColor;
            NSDictionary *colorData = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:color.hue], @"Hue",
                                       [NSNumber numberWithFloat:color.saturation], @"Saturation",
                                       [NSNumber numberWithFloat:color.brightness], @"Brightness",
                                       [NSNumber numberWithFloat:color.alpha], @"Alpha", nil];

            NSString* jsonString = [colorData JSONString];

:

 NSDictionary* dictionary = [coreDataEntity.fontColor objectFromJSONString];

                    NSNumber* hue =[dictionary objectForKey:kHueKey];
                    NSNumber* saturation =[dictionary objectForKey:kSaturationKey];
                    NSNumber* brightness =[dictionary objectForKey:kBrightnessKey];
                    NSNumber* alpha =[dictionary objectForKey:kAlphaKey];

                    UIColor* color = [UIColor colorWithHue:hue.floatValue saturation:saturation.floatValue brightness:brightness.floatValue alpha:alpha.floatValue];

                    textView.textColor = color;
0

, , - CIColor UIColor, :

:

CIColor(color: UIColor.blue).stringRepresentation

:

UIColor(ciColor: CIColor(string: databaseColorString))
0

All Articles