saving pictures in Core Data in #39;to-many relationship#39; environment(在“多对多关系环境中将图片保存在 Core Data 中)
问题描述
我正在处理我的小项目,该项目使用 Core Data 拍摄并保存照片,非常简单.我有两个实体;一个是人",另一个是图像".从 Person 到 Image 的关系是对多的,从 Image 到 Person 我也有反向关系.
I'm working on my small project which take pictures and save them, very simple, using Core Data. I have two entities; one is 'Person' and the other is 'Image'. the relationship from Person to Image is to-many and from Image to Person I got inverse relationship as well.
目前我只需要将多个图像(从 iPhone 相机拍摄或从库中选择)添加到一个 Person 实体.
All I need at the moment is to add multiple number of images (taken from iphone camera or chosen from library) to one Person entity.
我想知道是否有使用 Core Data 处理与 UIImage 的一对多关系的示例代码.
I'd like to know whether there is any kind of sample code that deals with to-many relationship with UIImage using Core Data.
非常感谢.
======================
=====================
其实我希望我能具体说明问题.我正在尝试做的是使用在多对关系中免费提供的访问方法来添加对象,例如
Actually I wish I could specify the problem. What i'm trying to do is using access methods that are given for free in to-many relationship to add object such as
- (void)addPersonImageObject:(PersonImage *)value;
我还实现了与 ImageToDataTransformer 相关的方法.
and also I've implemented methods related to ImageToDataTransformer.
我在这件事上花了将近一周的时间,我认为我需要一个指导示例代码,我可以先研究它以用于我的 add 项目.
I spent almost a week on this matter and I think I need a guiding sample code that I can study first for my add project.
推荐答案
除了将 UIImage
转换为 NSData
并返回之外,通过对多关系没有什么特别的与 UIImage
s.因此,任何包含一对多关系的核心数据教程都应该足够了.如果您需要示例代码,您可以查看 Apple 的 iPhoneCoreDataRecipes.
Besides converting the UIImage
to NSData
and back there is nothing special by having a to-many relationship with UIImage
s. So any core data tutorial that includes a to-many relationship should suffice. If you need sample code you can check out Apple's iPhoneCoreDataRecipes.
一些可帮助您入门的提示.假设我们有一个继承自 `UITableViewController' 的 PersonViewController
,它被调用如下:
A few pointers to help you get started. Let's pretend we have a PersonViewController
that inherits from a `UITableViewController' that gets called with something like:
PersonViewController *personViewController = [[[PersonViewController alloc] init] autorelease];
personViewController.person = ... // get the selected person
personViewController.managedObjectContext = self.managedObjectContext;
[self.navigationController pushViewController:personViewController animated:YES];
PersonViewController 具有以下属性:
PersonViewController has the following properties:
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) Person *person;
@property (nonatomic, copy) NSArray *images;
有一个添加按钮可以显示图像选择器
There is add button which would shod the image picker
- (void)insertNewObject {
UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init] autorelease];
imagePicker.delegate = self;
[self presentModalViewController:imagePicker animated:YES];
}
那么在回调imagePickerController:didFinishPickingMediaWithInfo:
我们可以:
// Get the image from the picker
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Transform the image to NSData
ImageToDataTransformer *transformer = [[[ImageToDataTransformer alloc] init] autorelease];
NSData *imageData = [transformer transformedValue:image];
// Create a new PersonImage entity and assign the image data
PersonImage *personImage = [NSEntityDescription insertNewObjectForEntityForName:@"PersonImage" inManagedObjectContext:self.managedObjectContext];
personImage.imageData = imageData;
// This is where we are adding the image to our person
[self.person addImagesObject:personImage];
// Core data save, however you want to do it.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
abort();
}
// simple trick to update the table view data source
self.images = nil;
[self.tableView reloadData];
// don't forget to dismiss the picker
[self dismissModalViewControllerAnimated:YES];
images
的实现只是为了简单
- (NSArray *)images {
if (images_) {
return images_;
}
// since we set self.images = nil when adding a new image we will get the list
// of all images from our person object.
images_ = [[self.person.images allObjects] retain];
return images_;
}
表格视图数据源方法只是:
The table view data source methods would just be:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.images.count;
}
但是,创建单元格并不重要.那时,如果我们想对图像做一些事情,我们可以在 tableView:didSelectRowAtIndexPath:
and however the cell is created doesn't really matter. At then if we wanted to do something with the images we can do something in tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// get the tapped person image
PersonImage *personImage = [self.images objectAtIndex:indexPath.row];
// get the image
ImageToDataTransformer *transformer = [[[ImageToDataTransformer alloc] init] autorelease];
UImage *image = [transformer reverseTransformedValue:personImage.imageData];
// do something with the image.
}
这篇关于在“多对多关系"环境中将图片保存在 Core Data 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在“多对多关系"环境中将图片保存在 Core Data 中
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01