Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 810877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:57:16+00:00 2026-05-15T00:57:16+00:00

I am trying to store a PNG image in a core data store backed

  • 0

I am trying to store a PNG image in a core data store backed by an sqlite database. Since I intend to use this database on an iPhone I can’t store NSImage objects directly. I wanted to use bindings and an NSValueTransformer subclass to handle the transcoding from the NSImage (obtained by an Image well on my GUI) to an NSData containing the PNG binary representation of the image. I wrote the following code for the NSValueTransformer :


+ (Class)transformedValueClass
{
    return [NSImage class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;   
}

- (id)transformedValue:(id)value
{
    if (value == nil) return nil;

    return [[[NSImage alloc] initWithData:value] autorelease];
}

- (id)reverseTransformedValue:(id)value
{
    if (value == nil) return nil;

 if(![value isKindOfClass:[NSImage class]])
 {
  NSLog(@"Type mismatch. Expecting NSImage");
 }

 NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];

 NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];

    return data;
}

The model has a transformable property configured with this NSValueTransformer. In Interface Builder a table column and an image well are both bound to this property and both have the proper value transformer name (an image dropped in the image well shows up in the table column). The transformer is registered and called every time an image is added or a row is reloaded (checked with NSLog() calls). The problem arises when I am trying to save the managed objects. The console output shows the error message:

[NSImage length]: unrecognized selector sent to instance 0x1004933a0

It seems like core data is using the value transformer to obtain the NSImage back from the NSData and then tries to save the NSImage instead of the NSData.

There are probably workarounds such as the one presented in this post but I would really like to understand why my approach is flawn.

Thanks in advance for your ideas and explanations.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T00:57:17+00:00Added an answer on May 15, 2026 at 12:57 am

    I eventually got it to work (best to sleep on it). There were two problems. The first one was that I specified the same value transformer both in IB bindings and in the core data model and it was redundant.

    My understanding is that IB bindings specified value transformers actually transform values between the views and the controllers whereas the core data value transformer acts as a middleman between the managed objects and the persistent store.

    To make the matter worse, the second problem was that my value transformer was upside down. The transformValue message is sent when the objects are saved (managed object -> store) and the reverseTransformedValue is sent when loading the values from the store which is somewhat the opposite direction of the transformers used when binding view objects to model objects. I got the hint from the core data programming guide where you can read the following somewhat cryptic note :

    Important: Although the default transformer is the transformer specified by NSKeyedUnarchiveFromDataTransformerName, this transformer is actually used in reverse. If you specify the default transformer explicitly, Core Data would use it “in the wrong direction.”

    Even though my program is working so far, I would like to have confirmation from someone with core data experience and I would appreciate if you could point me to some documentation on that matter that I might have missed.

    Thanks in advance.

    The working code :

    
    + (Class)transformedValueClass
    {
        return [NSData class];
    }
    
    + (BOOL)allowsReverseTransformation
    {
        return YES;   
    }
    
    - (id)reverseTransformedValue:(id)value
    {
        if (value == nil) return nil;
    
        return [[[NSImage alloc] initWithData:value] autorelease];
    }
    
    - (id)transformedValue:(id)value
    {
        if (value == nil) return nil;
    
        if(![value isKindOfClass:[NSImage class]])
        {
            NSLog(@"Type mismatch. Expecting NSImage");
        }
    
        NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
    
        NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
    
        return data;
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to store image form android sdcard to the sqlite database.
I am trying to store and retrieve photos from a MySql database. Can someone
I am trying to store more than 1 data item at a single index
I'm trying to store a lightly filtered copy of a database for offline reference,
I'm trying to store data in the following fashion with a string as the
I'm trying to store member function pointers by templates like this: (This is a
I'm trying to convert a fetched image from JPG to PNG using RMagick, resize
/*****UPDATED** ***/ r.com/YH3cm.png I am trying to figure out in the above image, how
I'm trying to write an image to the blob store. I've tried fetch_response =
I'm trying to create an image with the php libgd library and use the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.