将NSImage和NSData存储在Cocoa Core数据应用程序中

时间:2012-02-20 03:39:33

标签: objective-c cocoa core-data nsdata

我正在编写一个Cocoa App来显示一个二维浮动数组。我现在想将这些图像分配给Core Data中的实体属性,因此我使用了可转换的数据类型。

当我尝试使用可转换属性在实体(“TheEntity”)中存储NSImage(和/或NSData)时,问题就开始了,称之为“entityImage”。

这是我得到的错误:

CoreData: error: Serious application error.  Exception was caught during Core
Data change processing.  This is usually a bug within an observer of 
NSManagedObjectContextObjectsDidChangeNotification.  Cannot create BOOL from 
object 
<4d4d002a 00003d12 7f7f7e7c 7a777470 6c67615b 554e473f 372f271e 160d04fc
f3eae2d9 d1c9c1b9 b2aba59f 9994908c 89868482 81818182 8486898c 9094999f
a5abb2b9 c1c9d1d9 e2eaf3fc 040d161e 272f373f 474e555b 61676c70 74777a7c 
...

等(页面和页面)似乎是表示图像的NSData的十六进制值。

以下是包含评论的代码清单:

/*First create the greyscaleImage within the App (before storing to Core
  Data). The image size is Nx by Ny:  */

NSImage *greyscaleImage = [[NSImage alloc] initWithSize:NSMakeSize(Nx,Ny)]; 

/*The dataBitMapRep is constructed earlier (not shown) from a float array
using NSBitmapImageRep.  The greyscaleImage displays fine in an NSImage View,
I have been writing these to CALayers, so I won't go into those details here. */

/* "greyscaleImage" defined below can display fine within an NSImageView: */
[greyscaleImage addRepresentation:dataBitMapRep]; 

/* Create an instance of the Core-Data entity "TheEntity":  */
TheEntity *anEntityInstance = [NSEntityDescription insertNewObjectForEntityForName:@"TheEntity" inManagedObjectContext:moc];

/* Attempt to set value of entity "transformable" attribute (the line below causes the error): */
[anEntityInstance setEntityImage:greyscaleImage];

/* Have also tried using the bitmap rep directly (doesn't work): */
[anEntityInstance setEntityImage:dataBitMapRep];

/* Also tried the reverse by setting the tranformable value as NSData (rather than NSImage) and then switched my ValueTranformer output class accordingly as well as the value transformer and reverse transformer code (see the ImageTransformer Value-Transformer code below): */
 NSData *imgData = [greyscaleImage TIFFRepresentation];  
 // Store as NSData in Core Data:
 [anEntityInstance setEntityImage:imgData]; //still gives an error

当我采用上面直接描述的逆方法时(在通过将其返回类型切换到NSImage类来调整ValueTransformer之后,请参阅下面的代码),我得到了不同的错误风格(每个图像的一个列表,只有一个列出):

CoreData: error: Serious application error.  
Exception was caught during Core Data change processing.  
This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  
Cannot create BOOL from object <NSImage 0x102271f10 Size={125, 125} 
 Reps=("NSBitmapImageRep 0x102272fa0 Size={125, 125} 
ColorSpace=(not yet loaded) BPS=8 BPP=(not yet loaded) 
Pixels=125x125 Alpha=NO Planar=NO Format=(not yet loaded) CurrentBacking=nil
(faulting) CGImageSource=0x102272070")Cannot create BOOL from object 

Value Transformer是基本的。我还在各处设置NSLog以帮助排除故障(这些都没有显示):

#import "ImageTransformer.h"
@implementation ImageTransformer
+ (BOOL)allowsReverseTransformation {return YES;}
+ (Class)transformedValueClass {
    // Have switched back and forth here to try and diagnose bug:
    return [NSData class]; // also tried: return [NSImage class];
}
- (id)transformedValue:(id)value {    
    // If returning NSData class then transformedValue is NSData:
    NSData *data = [value TIFFRepresentation]; 
    return data;
    // If returning NSImage class then transformedValue is NSImage:
    // NSImage *imageRep = [[NSImage alloc] initWithData:value];
    // return imageRep;
}
- (id)reverseTransformedValue:(id)value {
    // If returning NSData class, reverseTransformedValue is NSImage:
    NSImage *imageRep = [[NSImage alloc] initWithData:value];
    return imageRep;
    // If returning NSNSImage class, reverseTransformedValue is NSData:
    // NSData *data = [value TIFFRepresentation]; 
    // return data;
}
@end

感谢您提供有关此问题发生原因的任何帮助或建议。我用Google搜索并检查了很多其他Stack文章,但没有找到这样的东西。

1 个答案:

答案 0 :(得分:0)

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

Cannot create BOOL from object ...
使用Image Well或Image Cell(NSImageView的子类)而不是我之前尝试写入的自定义视图时,不会发生

有关此问题的其他讨论,请参阅此post