为什么我的可转换核心数据属性不使用我的自定义 NSValueTransformer? [英] Why is my transformable Core Data attribute not using my custom NSValueTransformer?

查看:20
本文介绍了为什么我的可转换核心数据属性不使用我的自定义 NSValueTransformer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有相当简单数据模型的 Core Data 应用程序.我希望能够将 NSImage 的实例作为 PNG 位图 NSData 对象存储在持久存储中,以节省空间.

I have a Core Data app with a fairly simple data model. I want to be able to store instances of NSImage in the persistent store as PNG Bitmap NSData objects, to save space.

为此,我编写了一个简单的 NSValueTransformer 将一个 NSImage 转换为 PNG 位图格式的 NSData.我正在我的应用程序委托中使用此代码注册值转换器:

To this end, I wrote a simple NSValueTransformer to convert an NSImage to NSData in PNG bitmap format. I am registering the value transformer with this code in my App delegate:

+ (void)initialize
{
    [NSValueTransformer setValueTransformer:[[PNGDataValueTransformer alloc] init] forName:@"PNGDataValueTransformer"];
}

在我的数据模型中,我将图像属性设置为可转换,并指定 PNGDataValueTransformer 作为值转换器名称.

In my data model, I have set the image attribute to be Transformable, and specified PNGDataValueTransformer as the value transformer name.

但是,我的自定义值转换器没有被使用.我知道这一点,因为我已将日志消息放在我的值转换器的 -transformedValue:-reverseTransformedValue 方法中,这些方法没有被记录,并且正在保存到磁盘的数据是只是一个存档的 NSImage,而不是它应该是的 PNG NSData 对象.

However, my custom value transformer is not being used. I know this as I have placed log messages in my value transformer's -transformedValue: and -reverseTransformedValue methods which are not being logged, and the data that is being saved to disk is just an archived NSImage, not the PNG NSData object that it should be.

为什么这不起作用?

这是我的值转换器的代码:

Here is the code of my value transformer:

@implementation PNGDataValueTransformer

+ (Class)transformedValueClass
{
    return [NSImage class];
}

+ (BOOL)allowsReverseTransformation
{
    return YES;
}

- (id)transformedValue:(id)value
{
    if (value == nil) return nil;
    if(NSIsControllerMarker(value))
        return value;
    //check if the value is NSData
    if(![value isKindOfClass:[NSData class]])
    {
        [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSData instance", [value class]];
    }
    return [[[NSImage alloc] initWithData:value] autorelease];
}

- (id)reverseTransformedValue:(id)value;
{
    if (value == nil) return nil;
    if(NSIsControllerMarker(value))
        return value;
    //check if the value is an NSImage
    if(![value isKindOfClass:[NSImage class]])
    {
        [NSException raise:NSInternalInconsistencyException format:@"Value (%@) is not an NSImage instance", [value class]];
    }
    // convert the NSImage into a raster representation.
    NSBitmapImageRep* bitmap    = [NSBitmapImageRep imageRepWithData: [(NSImage*) value TIFFRepresentation]];
    // convert the bitmap raster representation into a PNG data stream
    NSDictionary* pngProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
    // return the png encoded data
    NSData* pngData             = [bitmap representationUsingType:NSPNGFileType properties:pngProperties];
    return pngData;
}

@end

推荐答案

事实证明,这实际上是框架中的一个错误.请参阅 Cocoa-Dev 邮件列表中一位 Apple 员工的帖子:

It turns out that this is actually a bug in the frameworks. See this post by an Apple employee on the Cocoa-Dev mailing list:

http://lists.apple.com/archives/Cocoa-dev/2009/Dec/msg00979.html

这篇关于为什么我的可转换核心数据属性不使用我的自定义 NSValueTransformer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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