iPhone - Why does the documentation say UIImageView is NSCoding compliant?
Asked Answered
R

2

12

Ideally an NSCoding compliant class will work as expected using encodeWithCoder: and initWithCoder: (at least I thought so till recently) without the developer having to bother about what goes on inside the routines (unless my idea of an NSCoding compliant class are totally screwed up!)

The UIImageView class is NSCoding compliant. So I should not have to bother how it will be serialized/de-serialized using the NSKeyedArchiver and NSKeyedUnarchiver classes. But every time I try and encode a UIImageView object, I get an error that UIImage does not recognize encodeWithCoder: method.

Now the UIImageView internally uses a UIImage object. But shouldn't the encoding have taken care of that itself?

Or is the NSCoding compliance specified in the documentation to just let the user know that they can implement the initWithCoder and encodeWithCoder methods?

Can someone please clarify this for me! I am thoroughly confused!

Robber answered 2/7, 2009 at 5:48 Comment(0)
I
33

The documentation is misleading -- UIImage does not conform to NSCoding as you've stated. You can work around it (in a primitive way) by doing the work yourself:

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

@implementation UIImage (NSCoding)
- (id)initWithCoder:(NSCoder *)decoder {
  NSData *pngData = [decoder decodeObjectForKey:@"PNGRepresentation"];
  [self autorelease];
  self = [[UIImage alloc] initWithData:pngData];
  return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
  [encoder encodeObject:UIImagePNGRepresentation(self) forKey:@"PNGRepresentation"];
}
@end
Inclose answered 2/7, 2009 at 13:58 Comment(5)
Thanks I am already doing that. Just wondering (ranting really) why Apple would mislead the developers! Same with the 3.0 not having any mention on the new method in UIImagePickerController in the docs.Robber
Definitely file a documentation bug in Radar (and add it to OpenRadar too so others can track the progress).Inclose
Thank you Nathan de Vries. Your answer help me a lot when dealing with CoreData!Obsolesce
Definitely a good & correct solution at the time. However, there have been some recent changes to iOS that affect this method. I link to a more detailed explanation in the new answer I posted.Carlocarload
will this correctly save and load the image's scale property?Marin
C
8

This question deserves an update since iOS 5.1 added functionality for NSCoding to UIImage, and Nathan de Vries answer will now cause warnings with the latest compilers.

This question offers a solution to work around the issue if your app supports iOS prior to 5.1. It does basically the same thing Nathan suggests, but checks whether the method already exists or not, rather than hard coding it.

Carlocarload answered 14/6, 2012 at 20:53 Comment(1)
Glad I scrolled all the way down the page! Cheers buddyPhilology

© 2022 - 2024 — McMap. All rights reserved.