我正在尝试将 PNG 图像存储在由 sqlite 数据库支持的核心数据存储中。由于我打算在 iPhone 上使用此数据库,因此无法直接存储 NSImage
对象。我想使用绑定和 NSValueTransformer 子类来处理从 NSImage (通过 GUI 上的图像井获得)到 NSData 的转码包含图像的 PNG 二进制表示形式。我为 NSValueTransformer
编写了以下代码:
+ (Class)transformedValueClass
{
return [NSImage class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if (value == nil) return nil;
return [[[NSImage alloc] initWithData:value] autorelease];
}
- (id)reverseTransformedValue:(id)value
{
if (value == nil) return nil;
if(![value isKindOfClass:[NSImage class]])
{
NSLog(@"Type mismatch. Expecting NSImage");
}
NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
return data;
}
该模型具有使用此 NSValueTransformer
配置的可转换属性。在 Interface Builder 中,表列和图像井都绑定到此属性,并且都具有正确的值转换器名称(放入图像井中的图像会显示在表列中)。每次添加图像或重新加载行时都会注册并调用转换器(使用 NSLog()
调用进行检查)。当我尝试保存托管对象时出现问题。控制台输出显示错误消息:
[NSImage length]:无法识别的选择器发送到实例 0x1004933a0
似乎核心数据正在使用值转换器从 NSData
获取 NSImage
,然后尝试保存 < code>NSImage 而不是 NSData
。
可能有一些解决方法,例如
预先感谢您的想法和解释。
I am trying to store a PNG image in a core data store backed by an sqlite database. Since I intend to use this database on an iPhone I can't store NSImage
objects directly. I wanted to use bindings and an NSValueTransformer
subclass to handle the transcoding from the NSImage
(obtained by an Image well on my GUI) to an NSData
containing the PNG binary representation of the image. I wrote the following code for the NSValueTransformer
:
+ (Class)transformedValueClass
{
return [NSImage class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)transformedValue:(id)value
{
if (value == nil) return nil;
return [[[NSImage alloc] initWithData:value] autorelease];
}
- (id)reverseTransformedValue:(id)value
{
if (value == nil) return nil;
if(![value isKindOfClass:[NSImage class]])
{
NSLog(@"Type mismatch. Expecting NSImage");
}
NSBitmapImageRep *bits = [[value representations] objectAtIndex: 0];
NSData *data = [bits representationUsingType:NSPNGFileType properties:nil];
return data;
}
The model has a transformable property configured with this NSValueTransformer
. In Interface Builder a table column and an image well are both bound to this property and both have the proper value transformer name (an image dropped in the image well shows up in the table column). The transformer is registered and called every time an image is added or a row is reloaded (checked with NSLog()
calls). The problem arises when I am trying to save the managed objects. The console output shows the error message:
[NSImage length]: unrecognized selector sent to instance 0x1004933a0
It seems like core data is using the value transformer to obtain the NSImage
back from the NSData
and then tries to save the NSImage
instead of the NSData
.
There are probably workarounds such as the one presented in this post but I would really like to understand why my approach is flawn.
Thanks in advance for your ideas and explanations.
发布评论
评论(2)
我最终让它工作了(最好睡在上面)。有两个问题。第一个是我在 IB 绑定和核心数据模型中指定了相同的值转换器,这是多余的。
我的理解是,IB 绑定指定值转换器实际上在视图和控制器之间转换值,而核心数据值转换器充当托管对象和持久存储之间的中间人。
更糟糕的是,第二个问题是我的价值转换器颠倒了。
transformValue
消息在保存对象时发送(托管对象 -> store),而reverseTransformedValue
在从存储加载值时发送,这在某种程度上是相反的方向将视图对象绑定到模型对象时使用的转换器。我从 核心数据编程指南,您可以在其中阅读以下有些神秘的注释:尽管我的程序到目前为止正在运行,但我希望得到具有核心数据经验的人的确认,如果您能为我指出一些我可能错过的有关该问题的文档,我将不胜感激。
提前致谢。
工作代码:
I eventually got it to work (best to sleep on it). There were two problems. The first one was that I specified the same value transformer both in IB bindings and in the core data model and it was redundant.
My understanding is that IB bindings specified value transformers actually transform values between the views and the controllers whereas the core data value transformer acts as a middleman between the managed objects and the persistent store.
To make the matter worse, the second problem was that my value transformer was upside down. The
transformValue
message is sent when the objects are saved (managed object -> store) and thereverseTransformedValue
is sent when loading the values from the store which is somewhat the opposite direction of the transformers used when binding view objects to model objects. I got the hint from the core data programming guide where you can read the following somewhat cryptic note :Even though my program is working so far, I would like to have confirmation from someone with core data experience and I would appreciate if you could point me to some documentation on that matter that I might have missed.
Thanks in advance.
The working code :
在transformedValue:中,你直接从NSData转到NSImage,但在reverseTransformedValue:中,你从NSImage转到NSBitmapImageRep再到NSData。所以两者不能一起工作,因为他们使用不同的方法。模型中的数据实际上是 NSBitmapImageRep 数据而不是 NSImage 数据。两者必须是完全相反的。
顺便说一句,我最近在另一篇文章中为此发布了一个变压器。您可以此处查看它。当然,您根本不需要自定义转换器,因为您只需使用 NSKeyedArchiver 即可。 NSImage 遵循 NSCoder 协议,因此它可以为您处理转换。
In transformedValue: you go straight from NSData to NSImage but in reverseTransformedValue: you go from NSImage to NSBitmapImageRep to NSData. So the two don't work together because they are using different methodologies. The data in your model is actually NSBitmapImageRep data and not NSImage data. The two need to be exact opposites of each other.
By the way, I posted a transformer for this recently in another post. You can see it here. And of course you don't need a custom transformer at all as you could just use NSKeyedArchiver. NSImage adheres to the NSCoder protocol so it would handle the transformation for you.