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];
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With