Pregunta

The code:

UIColor * color = [UIColor colorWithHue:0.3 saturation:0.2 brightness:0.2 alpha:1];
CGFloat r,g,b,a;

[color getRed:&r green:&g blue:&b alpha:&a];

NSData * colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
UIColor * unarchivedColor = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

[unarchivedColor getRed:&r green:&g blue:&b alpha:&a];


As expected on 32 bit builds, unarchivedColor and color have exactly the same rgb values. On arm64 builds, unarchivedColor and color start to differ around the 9th decimal place.

In other words, archiving/unarchiving a UIColor modifies the color on arm64... I need this not to happen. What's going on here and is there a way to fix this?

¿Fue útil?

Solución 2

Confirmed via an Apple Technical support incident that this is a bug relating to the archiving class on arm64. Their suggestion was to build a custom archiver class instead.

Otros consejos

The difference is probably because on 32-bit, CGFloat is a float while under 64-bit it is a double.

Try using double instead of CGFloat. Or just accept the fact that under 64-bit, you will get higher precision values.

This is no longer an issue (or perhaps never was an issue).

UIColor * color = [UIColor colorWithHue:0.3 saturation:0.2 brightness:0.2 alpha:1];
NSData * colorData = [NSKeyedArchiver archivedDataWithRootObject:color];
UIColor * unarchivedColor = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];

CGFloat r, g, b, h, s, a;
[color getHue:&h saturation:&s brightness:&b alpha:&a];
NSLog(@"HSBA %f %f %f %f", h, s, b, a);
[unarchivedColor getHue:&h saturation:&s brightness:&b alpha:&a];
NSLog(@"unarchived HSBA %f %f %f %f", h, s, b, a);

[color getRed:&r green:&r blue:&b alpha:&a];
NSLog(@"RGB %f %f %f %f", r, g, b, a);
[unarchivedColor getRed:&r green:&r blue:&b alpha:&a];
NSLog(@"unarchived RGB %f %f %f %f", r, g, b, a);

Results (formatted)

HSBA            0.300000 0.200000 0.200000 1.000000
unarchived HSBA 0.300000 0.200000 0.200000 1
RGB             0.200000 0.000000 0.160000 1.000000
unarchived RGB  0.200000 0.000000 0.160000 1.000000

However, constructing a UIColor object using HSBA values without arm64 selected, archiving it, and then unarchiving it under arm64 will result in different HSBA value when they are recovered due to different levels of precision with 64-bit.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top