将NSImage和NSData存储在Cocoa Core Data App中 [英] Storing NSImage and NSData in a Cocoa Core Data App

查看:114
本文介绍了将NSImage和NSData存储在Cocoa Core Data App中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Cocoa应用程序来显示一个2-d浮点数组。我现在想要将这些图像分配给Core Data中的实体属性,因此我使用可变换数据类型。



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



这是我得到的错误:



  CoreData:严重应用程序错误。 Core 
期间发生异常数据更改处理。这通常是
NSManagedObjectContextObjectsDidChangeNotification的观察器中的错误。无法从
对象
将BOOL; 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的十六进制值



以下是带有评论的代码列表:

  / *首先在App中创建greyscaleImage(在存储到Core 
Data之前)。图像大小是Ny的Nx:* /

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

/ * dataBitMapRep是使用NSBitmapImageRep从浮点数组
开始构建的(未显示)。 greyscaleImage在NSImage视图中显示很好,
我一直在写这些到CALayers,所以我不会在这里详细介绍这些细节。 * /

/ *greyscaleImage定义如下,可以在NSImageView中显示精细:* /
[greyscaleImage addRepresentation:dataBitMapRep];

/ *创建一个Core-Data实体的实例TheEntity:* /
TheEntity * anEntityInstance = [NSEntityDescription insertNewObjectForEntityForName:@TheEntityinManagedObjectContext:moc];

/ *尝试设置实体transformable属性的值(下面的行导致错误):* /
[anEntityInstance setEntityImage:greyscaleImage];

/ *还尝试使用位图表示直接(不工作):* /
[anEntityInstance setEntityImage:dataBitMapRep];

/ *通过将可变形值设置为NSData(而不是NSImage),然后相应地切换了ValueTranformer输出类以及值变换器和反向变换器代码(参见ImageTransformer Value-变换器代码如下):* /
NSData * imgData = [greyscaleImage TIFFRepresentation];
//在Core Data中存储为NSData:
[anEntityInstance setEntityImage:imgData]; //仍然给出一个错误



当我采取上面直接描述的逆方法(在调整ValueTransformer切换其返回类型为NSImage Class,见下面的代码),我得到不同的错误风格(每个图像的这些列表之一,只列出一个):

  CoreData:error:严重的应用程序错误。 
在核心数据更改处理期间捕获到异常。
这通常是NSManagedObjectContextObjectsDidChangeNotification的观察器中的一个错误。
无法从对象创建BOOL< NSImage 0x102271f10 Size = {125,125}
Reps =(NSBitmapImageRep 0x102272fa0 Size = {125,125}
ColorSpace = 8 BPP =(尚未加载)
Pixels = 125x125 Alpha = NO Planar = NO格式=(尚未加载)CurrentBacking = nil
(故障)CGImageSource = 0x102272070)无法从对象$ b创建BOOL $ b

价值变换器是基本的。我也设置NSLogs到处帮助排除故障(这些没有显示):

  #importImageTransformer.h
@implementation ImageTransformer
+(BOOL)allowReverseTransformation {return YES;}
+(Class)transformedValueClass {
//这里来回切换尝试和诊断bug:
return [NSData class]; // also tried:return [NSImage class];
}
- (id)convertedValue:(id)value {
//如果返回NSData类,那么convertedValue是NSData:
NSData * data = [value TIFFRepresentation];
return data;
//如果返回NSImage类,那么convertedValue是NSImage:
// NSImage * imageRep = [[NSImage alloc] initWithData:value];
// return imageRep;
}
- (id)reverseTransformedValue:(id)value {
//如果返回NSData类,reverseTransformedValue为NSImage:
NSImage * imageRep = [[NSImage alloc] initWithData:值];
return imageRep;
//如果返回NSNSImage类,reverseTransformedValue是NSData:
// NSData * data = [value TIFFRepresentation];
// return data;
}
@end

感谢您对此问题的任何帮助或建议发生。我已经在谷歌和检查了很多其他堆栈文章,没有发现这样的。

解决方案

,即

 无法从对象...创建BOOL ... 




请参阅此帖子进一步讨论此问题。


I am writing a Cocoa App to display a 2-d floating array. I now want to assign these images to an entity attribute in Core Data, so I use the transformable data type.

The problem starts when I try to store an NSImage (and / or NSData) within an Entity ("TheEntity") using a transformable attribute, call it "entityImage."

Here is the error I get:

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

and so on (pages and pages) which appear to be hexadecimal values for the NSData representing the image.

Below are code listings with comments:

/*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

When I take the inverse approach described directly above (after adjusting the ValueTransformer by switching its return type to NSImage Class, see code below), I get a different flavor of the error (one of these listings for each image, only one is listed):

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 

The Value Transformer is basic. I also set NSLogs everywhere to help troubleshoot (these are not shown):

#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

Thanks for any help or suggestions on why this problem occurs. I have Googled and checked lots of other Stack articles, have not found anything like this.

解决方案

I found that the error reported above, namely,

Cannot create BOOL from object ...

does not occur when using an Image Well or Image Cell (subclasses of NSImageView) rather than the Custom View that I was trying to write to earlier.

See this post for additional discussion on this problem.

这篇关于将NSImage和NSData存储在Cocoa Core Data App中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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