views:

818

answers:

2

I built a simple mac data entry tool I use with an iPhone application. I've recently added thumbnail which I added via an Image Well using simple bindings. Its a transformable data type which seems to work fine.

The iPhone application however won't show the images. The attribute isn't null but I can't get an image to appear. The following is for cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSManagedObject *entity = nil;
if ([self.searchDisplayController isActive])
    entity = [[self filteredListContent] objectAtIndex:[indexPath row]];
else
    entity = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [entity valueForKey:@"name"];
//cell.imageview.image = [UIImage imageNamed:@"ImageB.jpeg"]; //works fine
cell.imageView.image = [entity valueForKey:@"thumbnail"];//no error, but no file

return cell;

I'm thinking either the problem is with the transformable (I'm using the default NSKeyedUnarchiveFromData), or how I'm calling the thumbnail. I'm a newbie so any help would be greatly appreciated.

A: 

Of what type is thumbnail? Is it NSData? If so you will need to create a UIImage from that data.

Felix
The attribute type is Transformable (my starting point was Core Data Recipes sample from apple which also uses a transformable thumbnail). It uses a custom value transformer (ImageToDataTransformer) which wouldn't compile on the data entry application because of UIImage.
+3  A: 

Sounds like you are storing the image as an NSImage on the desktop and that object does not exist on the iPhone. Your desktop app needs to store the image in something that is portable, a PNG or JPG, etc. Then you will be able to load it back into your iPhone application as a UIImage.

Update re transformable

Sounds like you are still passing in a NSImage to the attribute and it is thinking you are handling it data. You need to convert it to a "standard" format first, like this:

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

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

I recommend writing custom accessors to handle this, like the following:

#ifdef IPHONEOS_DEPLOYMENT_TARGET

- (void)setImage:(UIImage*)image
{
  [self willChangeValueForKey:@"image"];

  NSData *data = UIImagePNGRepresentation(image);
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (UIImage*)image
{
  [self willAccessValueForKey:@"image"];
  UIImage *image = [UIImage imageWithData:[self primitiveValueForKey:@"image"];
  [self didAccessValueForKey:@"image"];
  return image;
}

#else

- (void)setImage:(NSImage*)image
{
  [self willChangeValueForKey:@"image"];
  NSBitmapImageRep *bits = [[image representations] objectAtIndex: 0];

  NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
  [myManagedObject setImage:data];
  [self setPrimitiveValue:data forKey:@"image"];
  [self didChangeValueForKey:@"image"];
}

- (NSImage*)image
{
  [self willAccessValueForKey:@"image"];
  NSImage *image = [[NSImage alloc] initWithData:[self primitiveValueForKey:@"image"]];
  [self didAccessValueForKey:@"image"];
  return [image autorelease];
}

#endif

This will give you a conditional compile and will store the data as NSData (in PNG format) that can be retrieved on any device.

Marcus S. Zarra
Mr Zarra, I have your book! Thank you for posting. I have followed your suggestion, but now I can't save my data entry file (the managed object doesn't save, with console message "[NSImage length]: unrecognized selector sent to instance") I have done the following:- added the a value transformer like here http://tinyurl.com/ylk8d3m (they are definitely being called)- added this to the Image Well Value binding (not ValuePath like in your book's example)- added it to the data model transformable attributeThanks for your help
Thank you SO much, that worked a treat once I got rid of the value transformer.