Thursday, March 5, 2009

UIImage and NSCoding

Here is a category to conform UIImage to NSCoding so you can archive it. This is untested - I hacked this up a few minutes in response to a question posed on Twitter. Please let me know if it needs any 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



This is also checked into Google Code if you prefer to grab it from there.



9 comments:

jpm said...

Jeff,

Excuse my potentially silly question, but why would you need this? Is it that much work to simply save the UIImage content in a file somewhere?

--Joao

Jeff LaMarche said...

Joao:

I can think of many situations where you would want to include image data when archiving an object. I didn't actually ask the person what they needed it for, but the main reason for it that I see is so that you can create model objects with UIImages in them that can be archived.

Jeff

Stephen said...

Jeff,

These files are missing from the iphoneparticles project you posted to Google. I was able to find them here quickly enough, but FYI.

See, we really are trying out your code ;) Thanks so much.

Steve

Ruy said...
This comment has been removed by the author.
Ruy said...

Hi,

I just wanted to add that I think you're missing a pair of braces in the header file around the category. It should read:

@interface UIImage(NSCoding) <NSCoding>

I am an absolute n00b to objective-C, so I could be totally wrong.

Ruy

born2pun said...

Responding to jpm's comment ("Excuse my potentially silly question, but why would you need this?"), one good reason is if you want to encode/decode a UIImageView. If you do that without a trick like this, you get an exception.

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

King Brian said...

Any thoughts on how to do this and preserve image orientation? UIImage is immutable, so it can't be done after the fact.

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels