ios - error while fetching image from photo library and save to coredata -
error below:
terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: '+entityforname: not locate entity named 'contacts' in model.'
code of fetching data photo library:
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { [picker dismissmodalviewcontrolleranimated:yes]; uiimage *img = [info objectforkey:@"uiimagepickercontrolleroriginalimage"]; nserror *error; nsmanagedobjectcontext *context; context = [self managedobjectcontext]; nsmanagedobject *object = [nsentitydescription insertnewobjectforentityforname:@"contacts" inmanagedobjectcontext:context]; nsdata *data=uiimagejpegrepresentation(img,0.0); [object setvalue:data forkey:@"image"]; [context save:nil]; [self fetchdata]; }
my fetch() method coding.
-(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 *object = objects[0]; data1 = [object valueforkey:@"image"]; image2 = [[uiimage alloc]initwithdata:data1]; imgviewofprofilepic.image=image2; }}
and coding of nsmanagedobjectcontext()
- (nsmanagedobjectcontext *)managedobjectcontext { nsmanagedobjectcontext *context; id delegate = [[uiapplication sharedapplication] delegate]; if ([delegate performselector:@selector(managedobjectcontext)]) { context = [delegate managedobjectcontext]; } return context; }
Comments
Post a Comment