我正在创建一个学生索引应用程序,您可以在其中保存学生的姓名,图片和角色。一切正常,但我的代码不会在我的核心数据模型中保存图像。我一整天都想弄明白。
您可以在此处找到我的整个项目:http://www21.zippyshare.com/v/26184330/file.html
#import "AddStudent.h"
#import "Studenten.h"
#import "Bild.h"
#import <QuartzCore/QuartzCore.h>
@interface AddStudent () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@end
@implementation AddStudent;
@synthesize delegate;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize selectedBetreuungsgrund;
@synthesize studentBetrGrundCell = _studentBetrGrundCell;
@synthesize vorschauImageView = _vorschauImageView;
- (void)viewWillAppear:(BOOL)animated {
_studentBetrGrundCell.textLabel.text = @"";
_studentBetrGrundCell.delegate = self;
_studentBetrGrundCell.managedObjectContext = self.managedObjectContext;
}
- (void)betrGrundWasSelectedOnPicker:(Betreuungsgrund *)betreuungsgrund {
self.selectedBetreuungsgrund = betreuungsgrund;
_studentBetrGrundCell.textLabel.text = self.selectedBetreuungsgrund.name;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)dismissKeyboard {
[self.view endEditing:TRUE];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.studentNachnameTextfeld.delegate = self;
self.studentVornameTextFeld.delegate = self;
_studentNachnameTextfeld.autocapitalizationType = UITextAutocapitalizationTypeSentences;
_studentVornameTextFeld.autocapitalizationType = UITextAutocapitalizationTypeSentences;
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[tgr setCancelsTouchesInView:NO];
[self.tableView addGestureRecognizer:tgr];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
- (IBAction)save:(id)sender
{
if (([_studentNachnameTextfeld.text isEqualToString:(@"")])||([_studentVornameTextFeld.text isEqualToString:(@"")])){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Sie müssen alle Namensfelder ausfüllen!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
}
else {
Student *student = [NSEntityDescription insertNewObjectForEntityForName:@"Student"
inManagedObjectContext:self.managedObjectContext];
student.vorname = _studentVornameTextFeld.text;
student.name = _studentNachnameTextfeld.text;
student.hatBetrGrund = selectedBetreuungsgrund; //Funzt trotzdem
student.bild.vorschauData = _vorschauImageView.image;
NSLog(@"Image Core Data: %@", _vorschauImageView.image);
NSLog(@"Image Core Data DB: %@", student.bild.vorschauData);
// NSLog(@"Betreuungsgrund: %@", student.hatBetrGrund);
[self.managedObjectContext save:nil]; // Eintrag in Datenbank
[self.delegate AddStudentSavePressed:self];
NSLog (@"Save Button bei AddBetrGrund pressed");
}
}
- (IBAction)albumButtonPressed:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Es steht kein Album zur Verfügung!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
return;
}
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.delegate = self;
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:controller animated:YES completion:nil];
}
- (IBAction)kameraButtonPressed:(id)sender
{
if (! [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Es steht keine Kamera zur Verfügung!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[alert show];
return;
}
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
controller.delegate = self;
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:nil];
}
- (IBAction)deleteBildPressed:(id)sender {
self.vorschauImageView.image = nil;
}
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
[picker dismissViewControllerAnimated:YES completion:nil];
_vorschauImageView.image = image;
_vorschauImageView.layer.cornerRadius = 10; // abgerundete Ecken
NSLog(@"Image Vorschau: %@", _vorschauImageView.image);
}
提前感谢您的帮助。干杯
答案 0 :(得分:1)
通常,您不应将图像保存在Core Data中。而是将其保存在磁盘上的文件中,并将路径保存到Core Data模型中的文件中。
这一行:
student.bild.vorschauData = _vorschauImageView.image;
使student
看起来像是与包含图像的其他对象有关系(bild
)。您需要创建该对象,将其插入上下文并将其与student
关联,然后才能使用它。