从照片库中提取图像时出错并保存到coredata

时间:2015-04-01 05:46:58

标签: ios objective-c xcode core-data

错误如下:

  

由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' + entityForName:无法找到名为' Contacts'在这个模型中。'

从照片库中提取数据的代码

- (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];

 }

我的fetch()方法编码。

-(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;

}}

编码NSManagedObjectContext()

- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
    context = [delegate managedObjectContext];
    }
  return context;
 }

0 个答案:

没有答案