I am trying to save a pic taken from the iphone camera to core data.
I have the UIImage taken from the camera. AFter that, I don't know what I'm supposed to do.
Each pic is associated with a question entity. Now a question entity does not have an image field. It has a relationship to an Image entity. This is so that each question can have many UIImages associated with it.
So I have created a question entity. Now how do I save the UIImage into core data into an Image entity, and how do I join it with a question entity? Do I need to create an Image entity? I have created an ImageToDataTransformer, but I do not know who calls that--core data or me? In my xcdatamodel, I have an Image entity. I put in ImageToDataTransformer there.
I am aware that I should be storing urls to the UIImage, but I want to understand how to store images. It seems kind of confusing to me.
Thanks
Question *question = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:managedObjectContext];
question.date = [NSDate date];
ImageToDataTransformer *transformer = [[ImageToDataTransformer alloc]init];
NSData *imageData = [transformer transformedValue:mm];//mm is an id, but it is the UIImage saved from the camera.
question.image = imageData;
I would suggest you make a QuestionImage entity (or something named similar) and give it a single string attribute named path. This path will store a location on disk where you've previously written out your image. Placing the image in the documents folder will ensure it does not get cleared with any cache dumps. In the implementation of your QuestionImage entity (or whatever you name it) you can implement prepareForDeletion and remove the image from the disk to make sure you don't have any un-referenced images hanging around.
Storing binary data in Core Data can be troublesome, and should be avoided where possible.