I am getting an image from photo library and saving it to core data and then showing it to an image view. This is correctly working. But when i run project again the image is not showing on my image view.
This is code for getting image from photo library
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissModalViewControllerAnimated:YES];
UIImage *img= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSManagedObjectContext *context=[self managedObjectContext];
NSManagedObject *newContact=[[NSManagedObject alloc] initWithEntity:[NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:context] insertIntoManagedObjectContext:context];
//newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Images" inManagedObjectContext:context];
NSData *data=UIImageJPEGRepresentation(img,0.0);
[newContact setValue:data forKey:@"image"];
[self fetchData];
}
This is the code of fetchData() method.
-(void)fetchData
{
NSManagedObjectContext *context=[self managedObjectContext];
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Contacts"
inManagedObjectContext:context]];
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:request error:&error];
if ([objects count] > 0) {
NSManagedObject *managedObject = objects[0];
data1 = [managedObject valueForKey:@"image"];
image2 = [[UIImage alloc]initWithData:data1];
_imageView.image=image2;
}
}
This is my viewDidLoad()
- (void)viewDidLoad
{
[super viewDidLoad];
[self fetchData];
// Do any additional setup after loading the view.
}