1

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.

4
  • firstly.. you must use the same key "thumnail" or "thumbnail" :) Commented Jan 14, 2013 at 12:13
  • I'm usign… simple.. "… encodeObject:image forKey …" where image is UIImage variable Commented Jan 14, 2013 at 12:15
  • my "Class" - pastie.org/5682577 Commented Jan 14, 2013 at 12:24
  • @TonyMkenu using simple encodeObject:image is also not working thats why i have commented that out in the code. Commented Jan 14, 2013 at 19:50

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have also used "[self setThumbnail:[UIImage imageWithData:[aDecoder decodeObjectForKey:@"thumbnail"]]];" but thats also not working
This is not fully encoding and decoding the UIImage because it is not encoding and then decoding the UIImage.scale property.
You can use imageWithData:scale: for that purposes
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;
}

7 Comments

just tried the code you provided. It didn't work. To my surprise, the data length in the console is zero.
Is it zero when encoding? Try logging the thumbnail object.
I have used "NSLog(@"Data length 1: %d", data.length);" in encodeWithCoder and NSLog(@"Data length 2: %d", data.length); in initWithCoder. Both logs gives '0' in console
If you get 0 on encoding that means that either the image is nil or it is failing to encode as PNG. Check that you have a valid image on encode. If you encode 0 bytes you will, of course, get 0 bytes on decode.
If your image is nil then you have a problem somewhere else. Maybe create a new question and post more code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.