Saving UIImage files with NSCoding by converting them to NSData

I am trying to save a UIImage fro an ABRecordRef in my own NSCoding conform Class. As far as i am now i can tell, that UIImage is not NSCoding conform so i tried the following work-around by using NSData instead:

- (id)initWithRecordRef:(ABRecordRef)record{
    //Getting the image
    if (ABPersonHasImageData(record)){
        CFDataRef imageData = ABPersonCopyImageData(record);
        image = [UIImage imageWithData:(NSData *) imageData];
        CFRelease(imageData);
    }


    return self;
}

     - (void)encodeWithCoder:(NSCoder *)aCoder{
            //encode NSData representation from UIImage
            tempData = UIImagePNGRepresentation(image);
            [aCoder encodeObject:tempData forKey:@"image"];
        }

    - (id)initWithCoder:(NSCoder *)aDecoder{
        //image
        tempData =          [aDecoder decodeObjectForKey:@"image"];
        image=              [UIImage imageWithData:[tempData valueForKey:@"image"]];
        [tempData release];

        return self;
    }

for some reason, the programm crashes as soon, as i try to save an instance of this class. What did i do wrong, or better: how can i accomplish what i want? Thanks!

Upvotes: 0

Views: 1439

Answers (1)

justin
justin

Reputation: 5831

I think the problem might be your [tempData release] call in the decoder. As far as I can see, there's no retain count through an allocation, so it's releasing an object that shouldn't be released. If that doesn't do the trick, I would set up a couple log statements in each method to see which are called before the crash occurs. This will give us a better idea where to focus

Upvotes: 1

Related Questions