Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving Image to core Data

I am a newbie related to core data. Could any one help me to provide proper steps/ tutorial showing how to save images to core data and its retrival too.I am able to store string data already, but my app crashes when trying to save image. For saving:

DataEvent *event = (DataEvent *)[NSEntityDescription insertNewObjectForEntityForName:@"DataEvent"
                                                          inManagedObjectContext:managedObjectContext];
NSURL *url2 = [NSURL URLWithString:@"xxxxxxxxxxxxxxx SOME URL xxxxxxxxxxxx"];

NSData *data = [[NSData alloc] initWithContentsOfURL:url2];
imageSave=[[UIImage alloc]initWithData:data];
NSData * imageData = UIImageJPEGRepresentation(imageSave, 100.0);    
[event setValue:self.imageSave forKey:@"pictureData"];

For retrival:

 DataEvent *event = (DataEvent *)[eventsArray objectAtIndex:indexPath.row];
 UIImage *image = [UIImage imageWithData:[event valueForKey:@"pictureData"]];
 UIImageView *imageViewMainBackGround = [[UIImageView alloc] 
 CGRect rect3=CGRectMake(0,2,100.0,100.0); 
 imageViewMainBackGround.frame = rect3;
 [cell.contentView addSubview:imageViewMainBackGround];
 [imageViewMainBackGround release];
like image 318
PUNEET GARG Avatar asked Apr 25 '12 07:04

PUNEET GARG


2 Answers

To save:

NSData *imageData = UIImagePNGRepresentation(myUIImage);

[newManagedObject setValue:imageData forKey:@"imageKey"];

And To Retrive Image:

NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"imageKey"]];
[[newCustomer yourImageView] setImage:image];

changed format

like image 167
Babul Mirdha Avatar answered Sep 29 '22 23:09

Babul Mirdha


error: reason = "The model used to open the store is incompatible with the one used to create the store"

solution:

delete the build project from simulator and clean from Product tab, now run the project.

like image 38
Engr Irfan Ali Avatar answered Sep 30 '22 01:09

Engr Irfan Ali