图像的核心数据可变属性的问题 [英] Trouble with Core Data Transformable Attributes for Images

查看:167
本文介绍了图像的核心数据可变属性的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持应该是一个非常简单的事情:有一个核心数据实体存储/显示(通过绑定)一个图像分配作为一个transformable属性。

I am stuck on what is supposed to be a very simple thing to do: have a Core Data Entity store / display (through bindings) an image assigned as a transformable attribute.

我已经阅读了很多相关的帖子在堆栈(例如,参见此处这里),但是在开发示例代码并研究了其他文章之后,它仍然有问题(例如,请参阅这里以及这里)。这与我之前的问题有关,我仍然没有解决。

I've read many related posts on the Stack (e.g., see here and here), but am still having trouble with it, after having developed sample code and researched other articles (e.g., see here as well as here). This is related to my earlier question, which I still have not resolved.

我创建了一个简单的基于文档的Core Data App来演示这个问题。 Core Data管理对象称为TheEntity和属性theImageAtt。核心数据中定义的实体如下所示(ImageValueTransformer是NSValueTransformer):

I created a simple doc-based Core Data App to demonstrate the problem. The Core Data managed object is called "TheEntity" and the attribute "theImageAtt." The entity as defined in Core Data is shown below (ImageValueTransformer is the NSValueTransformer):

我让XCode生成NSManagedObject子类的头文件和实现文件(我省略了name属性的代码,使它更简单):

I let XCode generate the NSManagedObject subclass header and implementation files (I left out the code for the "name" attribute to make it simpler):

//  TheEntity.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "ImageValueTransformer.h"
@interface TheEntity : NSManagedObject
@property (nonatomic, retain) NSImage * theImageAtt;
@end
-----------------------
//  TheEntity.m
#import "TheEntity.h"
@implementation TheEntity
@dynamic theImageAtt;
@end

下面是我的ImageValueTransformer的头文件和实现文件。

Below are the header and implementation files for my "ImageValueTransformer." Lots of examples of this on the Stack and elsewhere (the tiff rep is arbitrary).

//  ImageValueTransformer.h
#import <Foundation/Foundation.h>
@interface ImageValueTransformer : NSValueTransformer
@end
-------------------------------
//  ImageValueTransformer.m
#import "ImageValueTransformer.h"
@implementation ImageValueTransformer
+ (BOOL)allowsReverseTransformation {return YES;}
+ (Class)transformedValueClass {
    return [NSData class];  // have also tried: return [NSImage class]; 
}
- (id)transformedValue:(id)value {    
    NSData *data = [value TIFFRepresentation]; 
    return data;
}
- (id)reverseTransformedValue:(id)value {
    NSImage *imageRep = [[NSImage alloc] initWithData:value];
    return imageRep;
}
@end

Value Transformer可以通过分配它的一个实例在MyDocument.m中,但是最后,只要变换器头被导入到实体头(见上文)就没有关系了。我已经实验了这个,它不会删除我得到的错误。作为参考,有之前对是否需要注册值变换器(参见CornPuff和Brian Webster的意见)。

The Value Transformer can be initialized / registered by allocating an instance of it in MyDocument.m, but in the end, it doesn't matter that much as long as the transformer header is imported into the theEntity Header (see above). I have experimented with this and it does not remove the error I get below. For reference, there is earlier discussion on whether or not the value transformer needs to be registered (see the comments by CornPuff and Brian Webster).

回到手头的问题,来自Apple的一个好代码示例是这里显示了值变换器的另一个初始化,我也试过这个设置。

Back to the problem at hand, a good code example from Apple is here which shows an alternative initialization of the value transformer, I tried that setup too.

为了实现这一点,我有一个方法来加载一个测试图像,并将其分配给MyDocument.m中的transformable属性(来自NSTable中选定的Entity实例):



Putting this into action, I have a method to load a test image and assign it to the transformable attribute in MyDocument.m (from a selected Entity instance in an NSTable):

- (IBAction)addImg:(id)sender {
    NSImage *theImg = [[NSImage alloc] initWithContentsOfFile:@"/Desktop/testImage.jpg"];
    //[theImageView setImage:theImg]; // as a test, this displays ok
    // "theEntities" below are defined as an IBOutlet to my Array Controller:
    [theEntities setValue:theImg forKeyPath:@"selection.theImageAtt"];
    NSLog(@"after setting the image ..."); // this never logs
}

清零代码断开的位置

[theEntities setValue:theImg forKeyPath:@"selection.theImageAtt"];

出现错误:

Cannot create BOOL from object <4d4d002a 0003717e 8989898a 8a8a8b8b 8b8b8b8b
8a8a8a89 89898888 88878787 8a8a8a89 89898888 88888888 88888889 89898a8a
8a8a8a8a 8b8b8b88 88888787 87898989 8a8a8a89 89898a8a 8a8c8c8c 8b8b8b8a
8a8a8888 .... and so on for the size of the Image array ...

如果我注释掉然后我的NSTable填充正好(所以绑定和数组控制器似乎确定),但当然没有在NSImageView中的图像。

If I comment out the said line above then my NSTable populates just fine (so the bindings and array controller seem ok), but of course with no image in the NSImageView.

作为检查,转换Image Transformer中使用的代码按照预期工作(这是与值转换器分开测试的):

As a check, the conversion code used in the Image Transformer works as expected (this is tested separately from the value transformer):

// Image to Data Conversion:
NSImage *imageIn = [[NSImage alloc] initWithContentsOfFile:@"testImage.jpg"];
NSData *imgData = [imageIn TIFFRepresentation];
NSImage *imageOut = [[NSImage alloc] initWithData:imgData];
[theImageDisplay setImage:imageOut]; 

我缺少什么?

推荐答案

我发现上面报告的错误,即

I found that the error reported above, namely,

Cannot create BOOL from object ...

使用Image Well或Image Cell(NSImageView的子类),而不是我之前尝试写入的自定义视图。

does not occur when using an Image Well or Image Cell (subclasses of NSImageView) rather than the Custom View that I was trying to write to earlier.

所以现在我使用默认值变换器而不是自定义值变换器。这是一个可行的解决方案,但在学术上,很高兴知道默认自定义视图为什么绑定到Core Data属性(在Core Date模型中定义为可变换)时出现错误。

So for now I'm using the default value transformer rather than a custom value transformer. This is a workable solution, but academically speaking, it would be nice to know why the default Custom View led to errors when binding to a Core Data attribute (defined as transformable in the Core Date Model).

进一步深入一点,Image Well继承自NSImageView,所以至少有一个区别是它们在可编辑属性方面是不同的(Image Wells是可编辑的, n-Drop)。所以为了协调这两个,我设置我的自定义视图为可编辑,认为这可能解决问题:

Digging into this a little further, the Image Well inherits from NSImageView, so at least one difference is that they are distinct with regard to the "editable" property (Image Wells are editable which plays well with Drag-n-Drop). So in an attempt to reconcile these two, I set my Custom View to editable, thinking that this might resolve the problem:

theImageView = [[NSImageView alloc] init];
[theImageView setEditable:YES];

但它必须是别的,这不能解决上面的错误。至少现在,我有一个可行的解决方案。如果其他人遇到这种情况,我希望这些笔记是有帮助的!

But it must be something else, this does not resolve the error above. For now at least, I have a workable solution. Should others encounter this, I hope these notes are helpful!

这篇关于图像的核心数据可变属性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆