2

I am having trouble saving a photo into core data. I am trying to save it as an attribute set to 'Transformable' in an Entity. I have seen various discussions on this on SO and the consensus seems to be that in iOS5 and above, I don't need to use a coder as UIImage now conforms to NSCoding. I am getting an error when I try and save Core Data. Please see below the code I am using to save the photo...

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToSave;

    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {

        editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

        if (editedImage) {
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }

        // Convert image to Data for entry into Core Data
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(imageToSave)];

        // Add image to Core Data
        myEntity.attribute = imageData;

        NSError *error = nil;
        if (![managedObjectContext save:&error]) {
            NSLog(@"Error when saving core data");
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

    }

    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];
}
Ben Thompson
  • 4,743
  • 7
  • 35
  • 52
  • What is the error that you are getting? – Zalykr Apr 05 '12 at 12:17
  • 1
    Why do not you save image to Document directory and just save its path as NSString? Its way more efficient than you are doing.. Just did it few minutes ago.. – rohan-patel Apr 05 '12 at 12:19
  • The error I am getting is largely unhelpful: Error when saving core data. Unresolved error [null], [null]. – Ben Thompson Apr 05 '12 at 12:27
  • Why do not you follow the approach I suggested? Its really very efficient..see this: http://stackoverflow.com/questions/2090028/core-data-storing-images-iphone – rohan-patel Apr 05 '12 at 12:32
  • 1
    Can you confirm that the managedObjectContext and myEntity are not nil themselves? – borrrden Apr 05 '12 at 12:48
  • Sometimes there is nothing wrong with saving an image to CoreData. As long as the data isn't really large, it is fine to use CD for that. – LJ Wilson Apr 05 '12 at 13:23

2 Answers2

2

I agree to Joseph's answer. But looking at Apple's recommendation for storing image if your image is(Courtesy - Marcus S. Zarra's answer here):

  • Less than 100K;store as a binary property in your main table
  • Less than 1M; store as a binary property in a ancillary table to avoid over fetching
  • Greater than 1M; store on disk and store its file path in the Core Data table.

From your code what I see is you are trying to save image taken from camera to Core Data. We know images taken from Phone/iPad camera are approx 2.5 Mbs nowadays. So it is quite possible that you will get performance issues. So I would advice you to store image in a document directory and save it's path as NSString in your entity. It will be a more efficient way.

Community
  • 1
  • 1
rohan-patel
  • 5,772
  • 5
  • 45
  • 68
1

I have done this many times. Change the storage type from transformable to Binary Data and you should be fine.

You also want to keep a couple of things in mind. If the image is small (1MB or less), there should be no issue storing it in your main entity. If it is larger, you should have the image stored in an entity by itself for performance reasons. If the image is very large, you may want to consider storing it off in the documents directory like anonymous suggests above.

Joseph DeCarlo
  • 3,268
  • 23
  • 28
  • He is getting image from camera and we all know all iPhone/iPad camera captured images are approx 2.5 Mbs nowadays. So I can say he may get performance issues.. – rohan-patel Apr 05 '12 at 13:45