如果我执行以下操作,则可以保存正常:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)selectedImage editingInfo:(NSDictionary *)editingInfo {
box = (Box *)[NSEntityDescription insertNewObjectForEntityForName:@"Box" inManagedObjectContext:managedObjectContext];
// create an instance of a Box image, assign that instance to Box
//set that image to the selected image from the picker
BoxImage *image = [NSEntityDescription insertNewObjectForEntityForName:@"BoxImage" inManagedObjectContext:managedObjectContext];
box.boxImage = image;
[image setValue:selectedImage forKey:@"boxImage"];
但是每次有人选择图像时我都不想创建一个盒子实例....所以我有一个保存方法,它将从UIImage变量tempPhoto中设置盒子图像,如下所示:
-(IBAction)save
{
box = (Box *)[NSEntityDescription insertNewObjectForEntityForName:@"Box" inManagedObjectContext:managedObjectContext];
self.tempBoxCode = self.codeField.text;
//Set the box attributes
[box setCode:self.tempBoxCode];
BoxImage *image = [NSEntityDescription insertNewObjectForEntityForName:@"BoxImage" inManagedObjectContext:managedObjectContext];
box.boxImage = image;
[image setValue:tempPhoto forKey:@"boxImage"];
//commit this box
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
[self.view removeFromSuperview];
}
但它在[image setValue:tempPhoto forKey:@“boxImage”]上崩溃了; 。调试器中也没有错误消息。
非常感谢任何建议或建议:)
答案 0 :(得分:0)
这么多小时的挫败感和代码切换都归结为.self
我将tempPhoto更改为self.tempPhoto:)