本文介绍了在“多对多关系”环境中将图片保存在Core Data中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的小项目,它使用Core Data拍摄并保存它们非常简单。我有两个实体;一个是'人',另一个是'图像'。从Person到Image的关系是to-many,从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相机或从库中选择)到一个人实体

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.

我花了将近一个星期来处理这件事情,我想我需要一个指导性的示例代码,我可以先为我的添加项目学习。

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建立多对多关系并没有什么特别之处。所以任何包含to-many关系的核心数据教程都应该足够了。如果您需要示例代码,可以查看Apple的 iPhoneCoreDataRecipes

Besides converting the UIImage to NSData and back there is nothing special by having a to-many relationship with UIImages. 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.

一些指示可帮助您入门。让我们假设我们有一个 PersonViewController 继承自`UITableViewController',它被调用类似:

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 只是为了保留它简单

The implementation of images just to keep it simple

- (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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 23:04