如何将[String:UIImage]类型的字典存储到CoreData / UserDefaults中?

时间:2017-02-02 19:34:53

标签: ios swift

我有一个词典 productListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, ProductDetail.class); Uri currentProductUri = ContentUris.withAppendedId(InventoryContract.ProductEntry.CONTENT_URI, id); intent.setData(currentProductUri); startActivity(intent); } }); ,我想将它存储到CoreData / UserDefaults中。我怎么能这样做,什么是最好的选择?

尝试使用文件系统,但图片不会显示(offlineImages = [String: UIImage]()数组包含图片的本地网址):

offlineImages

2 个答案:

答案 0 :(得分:0)

在.xcdatamodeld文件中为类型为Transformable的字典创建属性。然后在生成的Entity子类中将类型从id更改为Dictionary

答案 1 :(得分:-1)

您可以将Image转换为NSData并存储到CoreData(二进制数据)中,就像这样

 NSData *imageData = UIImagePNGRepresentation(image);

 [selectedObject setValue:imageData forKey:@"imageEntity"]; // selectedObject refers to NSManagedObject

获取图片

NSManagedObject *selectedObj = [[self fetchedResultsController] objectAtIndexPath:indexPath];
UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];
[self.imageView setImage:image]