lundi 6 février 2012

UIImage et NSCoding

Voici une catégorie à se conformer aux UIImage NSCoding vous pouvez donc l'archive. Ce n'est pas testé - j'ai piraté ce jusqu'à quelques minutes en réponse à une question posée sur Twitter. S'il vous plaît laissez-moi savoir si elle a besoin des corrections:

UIImage-NSCoding.h
//
// UIImage-NSCoding.h

#import <Foundation/Foundation.h>

@interface UIImageNSCoding <NSCoding>
- (id)initWithCoder:(NSCoder *)decoder;
- (void)encodeWithCoder:(NSCoder *)encoder;
@end



UIImage-NSCoding.m
//
// UIImage-NSCoding.m

#import "UIImage-NSCoding.h"
#define kEncodingKey @"UIImage"

@implementation UIImage(NSCoding)
- (id)initWithCoder:(NSCoder *)decoder
{
if ((self = [super init]))
{
NSData *data = [decoder decodeObjectForKey:kEncodingKey];
self = [self initWithData:data];
}


return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
NSData *data = UIImagePNGRepresentation(self);
[encoder encodeObject:data forKey:kEncodingKey];
}

@end



Ceci est également vérifié dans Google Code si vous préférez le saisir à partir de là.

Aucun commentaire: