问题
I have seen different question on stackoverflow related to this topic. Some says that NSCoding does not conform with UIImage and other says that with iOS 5, it does conform.
I want to persist images in my app. I am using encode and decode methods and everything (title, labels etc) are persisting but NOT the images.
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:title forKey:@"title"];
[aCoder encodeObject:link forKey:@"link"];
[aCoder encodeObject:creator forKey:@"creator"];
[aCoder encodeObject:pubDate forKey:@"pubDate"];
// [aCoder encodeObject:thumbnail forKey:@"thumbnail"];
[aCoder encodeObject:UIImagePNGRepresentation(thumbnail) forKey:@"thumbnail"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
[self setTitle:[aDecoder decodeObjectForKey:@"title"]];
[self setLink:[aDecoder decodeObjectForKey:@"link"]];
[self setCreator:[aDecoder decodeObjectForKey:@"creator"]];
[self setPubDate:[aDecoder decodeObjectForKey:@"pubDate"]];
[self setThumbnail:[aDecoder decodeObjectForKey:@"thumbnail"]];
}
return self;
}
I am also using UIPNGRepresentation but its not working. Can someone help me with this.
Thanks.
回答1:
You're not encoding an image. UIImagePNGRepresentation() returns an NSData object. NSData conforms to NSSecureCoding. I haven't worked with that before, but the docs say that you have to decode it like this:
id obj = [decoder decodeObjectOfClass:[MyClass class] forKey:@"myKey"];
After Edit: The above doesn't appear to be necessary. I used the following code in a test app, and both approaches to encoding and decoding an image worked.
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"personName"];
[aCoder encodeObject:self.image2 forKey:@"thumbnail2"];
[aCoder encodeObject:UIImagePNGRepresentation(self.image1) forKey:@"thumbnail"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
[self setName:[aDecoder decodeObjectForKey:@"personName"]];
[self setImage2:[aDecoder decodeObjectForKey:@"thumbnail2"]];
[self setImage1:[UIImage imageWithData:[aDecoder decodeObjectForKey:@"thumbnail"]]];
}
return self;
}
As Aleph said, the problem must be elsewhere.
回答2:
Try this:
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:title forKey:@"title"];
[aCoder encodeObject:link forKey:@"link"];
[aCoder encodeObject:creator forKey:@"creator"];
[aCoder encodeObject:pubDate forKey:@"pubDate"];
NSData* data = UIImagePNGRepresentation(thumbnail);
NSLog(@"Data length: %d", data.length);
[aCoder encodeObject:data forKey:@"thumbnail"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self)
{
[self setTitle:[aDecoder decodeObjectForKey:@"title"]];
[self setLink:[aDecoder decodeObjectForKey:@"link"]];
[self setCreator:[aDecoder decodeObjectForKey:@"creator"]];
[self setPubDate:[aDecoder decodeObjectForKey:@"pubDate"]];
NSData* data = [aDecoder decodeObjectForKey:@"thumbnail"];
NSLog(@"Data length: %d", data.length);
[self setThumbnail:[UIImage imageWithData:data]];
}
return self;
}
来源:https://stackoverflow.com/questions/14314056/nscoding-persisting-uiimage