Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store an image in core data

I'm new to iOS. I've been trying to make an application that will store an image captured from the camera into CoreData. I now know how to store data like NSStrings, NSDate and other type but struggling to store an image. I've read so many articles saying you must write it to the disk and write to a file, but I can't seem to understand it.

The following code is the one i used to store other data to core data.

- (IBAction)submitReportButton:(id)sender {     UrbanRangerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];      managedObjectContext = [appDelegate managedObjectContext];      NSEntityDescription *entity = [NSEntityDescription entityForName:@"PotholesDB" inManagedObjectContext:appDelegate.managedObjectContext];     NSManagedObject *newPothole = [[NSManagedObject alloc]initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];       [newPothole setValue:self.relevantBody.text forKey:@"relevantBody"];     [newPothole setValue:self.subjectReport.text forKey:@"subjectReport"];     [newPothole setValue:self.detailReport.text forKey:@"detailReport"]; //    [newPothole setValue:self.imageView forKey:@"photo"];      NSDate *now = [NSDate date];     //NSLog(@"now : %@", now);     NSString *strDate = [[NSString alloc] initWithFormat:@"%@", now];     NSArray *arr = [strDate componentsSeparatedByString:@" "];     NSString *str;     str = [arr objectAtIndex:0];     NSLog(@"now : %@", str);      [newPothole setValue:now forKey:@"photoDate"];     [newPothole setValue:self.latitudeLabel.text forKey:@"latitude"];     [newPothole setValue:self.longitudeLabel.text forKey:@"longitude"];     [newPothole setValue:self.addressLabel.text forKey:@"streetName"];     [newPothole setValue:streeNameLocation forKey:@"location"];       NSError *error;     [managedObjectContext save:&error];      UIAlertView *ll = [[UIAlertView alloc] initWithTitle:@"Saving" message:@"Saved data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];      [ll show]; }  
like image 556
Sipho Koza Avatar asked May 22 '13 07:05

Sipho Koza


People also ask

How do I save an image in Core Data in Swift?

Creating the Model In that entity , create one attribute . Name it img and make sure the attribute type is Binary Data, then click on the img attribute and go to Data Model Inspector. Check the box Allows External Storage. By checking this box, Core Data saves a reference to the data which will make for faster access.

How do I save an object in Core Data?

To save an object with Core Data, you can simply create a new instance of the NSManagedObject subclass and save the managed context. In the code above, we've created a new Person instance and saved it locally using Core Data.

How do I save an image in Userdefaults?

It is, but it isn't possible to store an image as is in the user's defaults database. The defaults system only supports strings, numbers, Date objects, and Data objects. This means that you need to convert the image to a Data object before you can store it in the user's defaults database.


2 Answers

You can store images in Core Data using the Binary Data attribute type. However you should be aware of a few things:

  • Always convert your UIImage to a portable data format like png or jpg For example:

    NSData *imageData = UIImagePNGRepresentation(image);

  • Enable "Allows external storage" on this attribute description Core Data will move the data to an external file if it hits a certain threshold. This file is also completely managed by Core Data, so you don't have to worry about it.

  • If you run into performance issues, try moving the Binary Data attribute to a separate entity.

  • You should abstract the conversion to NSData behind the interface of your NSManagedObject subclass, so you don't have to worry about conversions from UIImage to NSData or vice versa.

  • If your images are not strongly related to the entities in your model, I would suggest storing them outside of Core Data.

like image 98
jansenmaarten Avatar answered Oct 02 '22 20:10

jansenmaarten


In xcdatamodelId subclass declare image entity as NSData... you can't use UIImage format because image data is in binary format.

@property (nonatomic, retain) NSData *imag; 

In Implementation file.. convert UIImage to NSData

UIImage *sampleimage = [UIImage imageNamed:@"sampleImage.jpg"];  NSData *dataImage = UIImageJPEGRepresentation(sampleimage, 0.0); 

Then finally save it

[obj setValue:dataImage forKey:@"imageEntity"]; // obj refers to NSManagedObject 
like image 34
Jemythehigh Avatar answered Oct 02 '22 21:10

Jemythehigh