保存照片并使用Core Data显示

时间:2013-12-23 07:50:26

标签: ios objective-c core-data

今天在我正在处理的应用程序中,它允许用户添加个人资料照片但我需要将其保存在核心数据中,每次我认为我保存它不会出现。我的代码列在下面。

- (void)viewDidLoad
{
NSArray *array = [[NSArray alloc] initWithObjects:@"Associate", @"Part-Time", @"Area", @"Regional Cross Checker", @"National Cross Checker", @"Scouting Director", @"Front Office", nil];
self.PickerData = array;

[super viewDidLoad];
// Do any additional setup after loading the view.                                                                                                                                                                                   

if (self.device) {
    [self.scoutNameText setText:[self.device valueForKey:@"name"]];
    [self.scoutContactText setText:[self.device valueForKey:@"number"]];
    [self.scoutTeamText setText:[self.device valueForKey:@"team"]];
    [self.email setText:[self.device valueForKey:@"email"]];
    [self.pick1 setText:[self.device valueForKey:@"pick1"]];
    [self.pick2 setText:[self.device valueForKey:@"pick2"]];
    [self.pick3 setText:[self.device valueForKey:@"pick3"]];
    [self.pick4 setText:[self.device valueForKey:@"pick4"]];
    [self.pick5 setText:[self.device valueForKey:@"pick5"]];
    [self.pick6 setText:[self.device valueForKey:@"pick6"]];
    [self.jobtitle setText:[self.device valueForKey:@"jobtitle"]];
    _photo.image = [UIImage imageWithData:[self.device valueForKey:@"photo"]];

}

}


- (IBAction)ProfilePhoto:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    [picker setSourceType:UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker animated:YES completion:nil];
}
else {
    [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [picker setDelegate:self];
    [self presentViewController:picker animated:YES completion:nil];
}

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

// self.imageView.image = chosenImage;
_image = [info objectForKey:UIImagePickerControllerEditedImage];
_imageData = UIImagePNGRepresentation(_image);
[_photo setImage:_image];

//NSManagedObjectContext *context = [self managedObjectContext];
//[self.device setValue:self.scoutNameText.text forKey:@"name"];
//[self.device setValue:self.imageData forKey:@"photo"];

[picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (IBAction)save:(id)sender {

// Update existing device
NSManagedObjectContext *context = [self managedObjectContext];
[self.device setValue:self.scoutNameText.text forKey:@"name"];
[self.device setValue:self.scoutContactText.text forKey:@"number"];
[self.device setValue:self.scoutTeamText.text forKey:@"team"];
[self.device setValue:self.pick1.text forKey:@"pick1"];
[self.device setValue:self.pick2.text forKey:@"pick2"];
[self.device setValue:self.pick3.text forKey:@"pick3"];
[self.device setValue:self.pick4.text forKey:@"pick4"];
[self.device setValue:self.pick5.text forKey:@"pick5"];
[self.device setValue:self.pick6.text forKey:@"pick6"];
[self.device setValue:self.email.text forKey:@"email"];
[self.device setValue:self.jobtitle.text forKey:@"jobtitle"];
[self.device setValue:_imageData forKey:@"photo"];



 NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}



[self dismissViewControllerAnimated:YES completion:nil];

}

如果您要求查看其他任何内容,我将不仅仅是快乐。感谢您的全力支持。

1 个答案:

答案 0 :(得分:0)

StackOverflow上有很多关于如何保存照片和使用核心数据的问题。

Saving Photo in Core Data

是那个立刻出现的人。但是,我怀疑你是否已经完成了基本的调试:

  1. 保存__imageData
  2. nil NSManagedObject 当你尝试保存时,
  3. device nil?
  4. 如何在保存或重启后从Core Data中获取device
  5. 您是否在磁盘上查看实体或照片是否已保存?