此代码应该保存核心数据 . 图像存储在它自己的实体中并且被另一个实体所喜欢(关系)
NSEntityDescription *imageEntity = [ NSEntityDescription insertNewObjectForEntityForName:kImage inManagedObjectContext:self.context];
[imageEntity setValue:self.image forKey:kPicture];
[newPerson setValue:imageEntity forKey:kPicture];
NSError *error;
if (![self.context save:&error]) {
NSLog(@"Whoops, couldn't save new associate: %@", [error localizedDescription]);
}
在这里,我尝试将其恢复,将其放在自定义单元格的uiimageview中
Person *crt = [self.fetchController objectAtIndexPath:indexPath];
cell.name.text = crt.name;
Image *img = crt.picture;
cell.image.image = img.picture;
这仅在首次添加图片时有效...重新启动应用后图像不会再次显示..为什么会这样?它被设置为可转换属性
1 回答
您只能将
UIImage
添加为二进制数据 . 所以你需要做的是将你的实体的属性类型设置为Binary Data
,并使用CoreGraphics'
方法UIImagePNGRepresentation(UIImage *image)
,它返回NSData
并以这种方式保存它 . 如果要从数据库中提取它,则会获得NSData
并使用[UIimage imageWithData:(NSData *data)]
方法创建图像 .但我认为最好的实现是只存储数据库中图像的本地路径,但实际文件存储在应用程序的Documents文件夹中的文件夹中......只是一个建议 .