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

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

问题描述

我有一个Core Data应用程序与一个相当简单的数据模型。我想要能够将NSImage的实例存储在持久存储中作为PNG Bitmap NSData对象,以节省空间。



为此,我写了一个简单的NSValueTransformer来转换一个NSImage到NSData的PNG位图格式。我在我的应用程序委托中注册带有此代码的值变换器:

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



在我的数据模型中,我将image属性设置为Transformable指定 PNGDataValueTransformer 作为值转换器名称。



但是,我的自定义值变换器未使用。我知道这是因为我把日志消息放在我的值变换器的 -transformedValue: -reverseTransformedValue 方法,不是记录,而保存到磁盘的数据只是一个归档的NSImage,而不是它应该是的PNG NSData对象。



为什么这不工作? p>

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

  @implementation PNGDataValueTransformer 

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

+(BOOL)allowReverseTransformation
{
return YES;
}

- (id)convertedValue:(id)value
{
if(value == nil)return nil;
if(NSIsControllerMarker(value))
返回值;
//检查值是否为NSData
if(![value isKindOfClass:[NSData class]])
{
[NSException raise:NSInternalInconsistencyException format:@Value(% @)不是NSData实例,[value class]];
}
return [[[NSImage alloc] initWithData:value] autorelease];
}

- (id)reverseTransformedValue:(id)value;
{
if(value == nil)return nil;
if(NSIsControllerMarker(value))
返回值;
//检查值是否为NSImage
if(![value isKindOfClass:[NSImage class]])
{
[NSException raise:NSInternalInconsistencyException format:@Value %@)不是NSImage实例,[value class]];
}
//将NSImage转换为栅格表示。
NSBitmapImageRep * bitmap = [NSBitmapImageRep imageRepWithData:[(NSImage *)value TIFFRepresentation]];
//将位图光栅表示转换为PNG数据流
NSDictionary * pngProperties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:NSImageInterlaced];
//返回png编码数据
NSData * pngData = [bitmap representationUsingType:NSPNGFileType properties:pngProperties];
return pngData;
}

@end


解决方案>

事实证明,这实际上是一个在框架中的错误。 Apple员工在Cocoa-Dev邮件列表中查看此帖子:



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


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.

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"];
}

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

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.

Why is this not working?

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

解决方案

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天全站免登陆