為什么我的可轉換Core Data屬性不使用我的自定義NSValueTransformer? - Why is my transformable Core Data attribute not using my custom NSValueTransfor

為什么我的可轉換Core Data屬性不使用我的自定義NSValueTransformer? - Why is my transformable Core Data attribute not using my custom NSValueTransfor,第1张

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.

我有一個Core Data應用程序,它具有相當簡單的數據模型。我希望能夠將持久存儲中的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:

為此,我編寫了一個簡單的NSValueTransformer,將NSImage轉換為PNG位圖格式的NSData。我在我的App委托中使用此代碼注冊值轉換器:

  (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.

在我的數據模型中,我將image屬性設置為Transformable,並將PNGDataValueTransformer指定為值轉換器名稱。

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.

但是,我的自定義值轉換器未被使用。我知道這一點,因為我在我的值變換器的-transformedValue:和-reverseTransformedValue方法中放置了日志消息,這些方法沒有被記錄,並且保存到磁盤的數據只是一個存檔的NSImage,而不是它應該是的PNG NSData對象。 。

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

6 个解决方案

#1


25  

If I'm not mistaken, your value transformer has a reversed direction. I do the same thing in my application, using code like the following:

如果我沒弄錯的話,你的價值變換器的方向相反。我在我的應用程序中執行相同的操作,使用如下代碼:

  (Class)transformedValueClass 
{
 return [NSData class]; 
}

  (BOOL)allowsReverseTransformation 
{
 return YES; 
}

- (id)transformedValue:(id)value 
{
 if (value == nil)
  return nil;

 // I pass in raw data when generating the image, save that directly to the database
 if ([value isKindOfClass:[NSData class]])
  return value;

 return UIImagePNGRepresentation((UIImage *)value);
}

- (id)reverseTransformedValue:(id)value
{
 return [UIImage imageWithData:(NSData *)value];
}

While this is for the iPhone, you should be able to swap in your NSImage code at the appropriate locations. I simply haven't tested my Mac implementation yet.

雖然這適用於iPhone,但您應該能夠在適當的位置交換NSImage代碼。我還沒有測試過我的Mac實現。

All I did to enable this was to set my image property to be transformable within the data model, and specify the name of the transformer. I didn't need to manually register the value transformer, as you do in your initialize method.

我所做的就是將我的image屬性設置為可在數據模型中轉換,並指定轉換器的名稱。我不需要像在 initialize方法中那樣手動注冊值轉換器。

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复