1

What is the SIMPLEST way to save a UIImageView to a CoreData Database. I have tried this:

Save:

 UIImage *image = imageView.image;
    NSData *imageData = UIImagePNGRepresentation(image);
    [newContact setValue:imageData forKey:@"imageViewFinal"];

Retrive:

  imageView.image = [matches valueForKey:@"imageViewFinal"];

and I have added an Attribute called 'imageViewFinal' with a Binary Data type.

PROBLEM:

When I go to build it and click the save button, the app crashes, what's the problem?

Thanks, Seb.

Seb
  • 53
  • 10
  • Could you share the crash log? I think you should use the transformable data type to store the NSImage directly into Core Data. – Lorenzo B Dec 22 '12 at 14:09
  • brad (@bradlarson) as already pointed out it here http://stackoverflow.com/questions/3908910/how-should-i-store-uiimages-within-my-core-data-database – Lorenzo B Dec 22 '12 at 14:11
  • Crash Log: 2012-12-23 12:26:21.783 OH&S TUE[24213:c07] Unresolved error Error Domain=NSCocoaErrorDomain Code=134100 "The operation couldn’t be completed. (Cocoa error 134100.)" UserInfo=0x777b5e0 {metadata={ NSPersistenceFrameworkVersion = 419; NSStoreModelVersionHashes = { SWMS = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; NSStoreUUID = "1902A7D8-37E9-493F-8586-A76F5642AF79"; "_NSAutoVacuumLevel" = 2; – Seb Dec 23 '12 at 01:27
  • }, reason=The model used to open the store is incompatible with the one used to create the store}, { metadata = { NSPersistenceFrameworkVersion = 419; NSStoreModelVersionHashes = { SWMS = ; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( "" ); NSStoreType = SQLite; – Seb Dec 23 '12 at 01:28
  • 1
    Your problem is that you changed your model file after installing the app. Core Data then doesn't know how to modify the database to take into account the changes. The simplest solution is just to delete the app from the Simulator or device and reinstall. – Sherman Lo Dec 23 '12 at 02:26

1 Answers1

1

What @ShermanLo said is right, the crash log shows that you've modified your model but did not handle the conflict between the old & new store model versions in the right way.

So just delete your App in your device/simulator, and rebuild/run it.

Note: Whenever you've modified your models, you need to do it this way unless you offer an approach to handle. There're many related QAs on SO. :)

Kjuly
  • 34,476
  • 22
  • 104
  • 118