UIColor crash when attempt to archive UIColor object
A UIColor object created with a pattern image will reliabl开发者_如何学编程y crash when you attempt to archive that object.
UIColor *patternColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"test.png"]];
[NSKeyedArchiver archiveRootObject:patternColor toFile:[NSHomeDirectory() stringByAppendingFormat:@"/archive"]];
Reposting my previous comment as an answer per @deanWombourne suggestion...
I had the same problem. Posted a clearer explanation here...
Saving UIColor colorWithPatternImage:image UIColor to Core Data using NSKeyedArchiver
OK, second attempt - I've deleted my first.
@Audrey - you need to post the console exception trace in your question - it's really very helpful and would have prevented the last 10 minutes of discussion in the comments.
I've built a test app with your code in (apart from the method swizzling that you insist has nothing to do with the problem but won't show us) and it does crash.
However, it tells me exactly why :
2011-09-19 12:51:10.346 test[50884:40b] *** Assertion failure in -[UICGColor encodeWithCoder:], /SourceCache/UIKit_Sim/UIKit-1448.89/UIColor.m:894
2011-09-19 12:51:10.447 test[50884:40b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only support RGBA or the White color space, this method is a hack.'
You can only serialise certain UIColor objects.
Instead of serialising the UIColor, you need to store the image that the pattern was created from. Do this in your encodeWithCoder:
Then, in your initWithCoder:
you should dearchive the image and create a new UIColor from that.
Though, just to be completely clear, I can't say if this is the same crash you are seeing. Without any sort of error messages this answer is just a guess. Please try to be helpful; people have asked for more information and you seem reluctant to give it.
精彩评论