参考:
[1].https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i
@interface PKAnimationItem : NSObject<NSCoding>
{
NSString* title_;
NSString* type_;
NSString* sn_;
NSString* imageName_;
UIImage* smallImage_;
UIImage* largeImage_;
}
@property(nonatomic,retain) NSString*title;
@property(nonatomic,retain) NSString*type;
@property(nonatomic,retain) NSString*sn;
@property(nonatomic,retain) NSString*imageName;
@property(nonatomic,retain) UIImage*smallImage;
@property(nonatomic,retain) UIImage*largeImage;
@end
static NSString* kAnimationItemTitle = @"KAMTitle";
static NSString* kAnimationItemType = @"kAMType";
static NSString* kAnimationItemImageName = @"kAMImageName";
static NSString* kAnimationItemSn = @"kAMSN";
@implementation PKAnimationItem
@synthesize title = title_;
@synthesize type = type_;
@synthesize sn = sn_;
@synthesize imageName = imageName_;
@synthesize smallImage = smallImage_;
@synthesize largeImage = largeImage_;
- (void)dealloc
{
[title_ release];
[type_ release];
[sn_ release];
[imageName_ release];
[smallImage_ release];
[largeImage_ release];
[super dealloc];
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.title forKey:kAnimationItemTitle];
[aCoder encodeObject:self.type forKey:kAnimationItemType];
[aCoder encodeObject:self.imageName forKey:kAnimationItemImageName];
[aCoder encodeObject:self.sn forKey:kAnimationItemSn];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
self.title = [aDecoder decodeObjectForKey:kAnimationItemTitle];
self.type = [aDecoder decodeObjectForKey:kAnimationItemType];
self.sn = [aDecoder decodeObjectForKey:kAnimationItemSn];
self.imageName = [aDecoder decodeObjectForKey:kAnimationItemImageName];
self.largeImage = [PKUtils animationImageWithName:self.imageName];
NSString* smallImageName = [[self.imageName stringByDeletingPathExtension] stringByAppendingString:@"_s.gif"];
self.smallImage = [PKUtils animationImageWithName:smallImageName];
self.smallImage = self.smallImage==nil?self.largeImage:self.smallImage;
}
return self;
}
- (NSString *)description
{
NSMutableString* des = [NSMutableString stringWithCapacity:0];
[des appendFormat:@"\n|---------start-------|\n"];
[des appendFormat:@"|title=%@\n",self.title];
[des appendFormat:@"|type=%@\n",self.type];
[des appendFormat:@"|imageName=%@\n",self.imageName];
[des appendFormat:@"|sn=%@\n",self.sn];
[des appendFormat:@"\n|--------end--------|\n"];
return des;
}
@end
使用
1.编码:
[NSKeyedArchiver archiveRootObject:PKAnimationItemArr toFile:arichverPath];
2.解码:
NSArray* animationArr = [NSKeyedUnarchiver unarchiveObjectWithFile:arichverPath];