核心数据图像将不会加载到NSTableView图像单元格 [英] Core Data Image Won't Load Into NSTableView Image Cell

查看:105
本文介绍了核心数据图像将不会加载到NSTableView图像单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我将图像存储到我的Core Data模型(工作正常)。如果我设置我的视图有一个NSImageView并绑定其数据到控制器键:选择和modelKeyPath:myImagePath,它的工作原理。它将显示所选行的每个图像。



然后我在NSTableView中创建一个新列,并将一个图像单元格拖动到列上。然而,我不能得到我的核心数据绑定,让图像显示在单元格中。我已经尝试绑定值和数据,但没有运气。



由于我确信图像存储正确,我做错了我的绑定,以防止图像



(我的背景:新的Cocoa开发人员)

解决方案

我不知道发生了什么,导致你的问题,但有一个简单的方法找出来。连接一个NSValueTransformer到绑定。然后在这个变换器中,你可以记录东西,看看你是否传递一个nil值,或者你可以将你的数据值转换为NSImage并传递回...基本上你可以做任何你想要的变压器类。这是我在核心数据模型中对图像数据使用的一个。

  @interface DataToImageTransformer:NSValueTransformer {

}

@end


@implementation DataToImageTransformer

+(Class)convertedValueClass {
return [NSImage class ];
} //从convertedValue返回值的类:

+(BOOL)allowReverseTransformation {
return YES;
} //如果是YES则必须也有reverseTransformedValue:

- (id)transformedValue:(id)value {
if(value == nil || [value length] < 1)return nil;
NSImage * i = nil;
if([value isKindOfClass:[NSData class]]){
i = [NSKeyedUnarchiver unarchiveObjectWithData:value];
}
return i;
}

- (id)reverseTransformedValue:(id)value {
if(value == nil)return nil;
NSData * d = nil;
if([value isKindOfClass:[NSImage class]]){
d = [NSKeyedArchiver archivedDataWithRootObject:value];
}
return d;
}

@end



在AppController类中,变换器:

  +(void)initialize {
DataToImageTransformer * transformer = [[DataToImageTransformer alloc] init];
[NSValueTransformer setValueTransformer:transformer forName:@DataToImageTransformer];
[transformer release];
}

然后在Interface Builder中,将DataToImageTransformer放入绑定。现在你可以控制绑定,并可以做,如我前面在变压器类中解释。注意,我使用NSKeyedArchiver将一个NSImage转换为数据,然后返回,但你可以使用tiffRepresentation或任何其他方法,你想要在它的位置。


In my code I am storing an image into my Core Data model (works fine). If I set up my view to have an NSImageView and bind its Data to Controller Key: selection and modelKeyPath: myImagePath, it works. It will show each image for the selected row.

I then create a new column in my NSTableView and drag an image cell onto the column. However, I cannot get my Core Data bindings to have the image show up in the cell. I have tried binding both value and data, but no luck.

Since I am sure the image is stored properly, what am I doing wrong in my binding to prevent the image from showing up in the table cell?

Thanks so much.

(My background: new Cocoa developer who has recently gone through the entire Hillegass book.)

解决方案

I'm not sure what is happening to cause your problem but there's an easy way to find out. Hook up an NSValueTransformer to the binding. Then in that transformer you can log stuff to find out if you're passing a nil value, or you can transform your data value into an NSImage and pass that back… basically you can do whatever you want in the transformer class. Here's one I use on image data in a core-data model.

@interface DataToImageTransformer : NSValueTransformer {

}

@end


@implementation DataToImageTransformer

+ (Class)transformedValueClass {
    return [NSImage class];
} // the class of the return value from transformedValue:

+ (BOOL)allowsReverseTransformation {
    return YES;
} // if YES then must also have reverseTransformedValue:

- (id)transformedValue:(id)value {
    if (value == nil || [value length] < 1) return nil;
    NSImage* i = nil;
    if ([value isKindOfClass:[NSData class]]) {
        i = [NSKeyedUnarchiver unarchiveObjectWithData:value];
    }
    return i;
}

- (id)reverseTransformedValue:(id)value {
    if (value == nil) return nil;
    NSData* d = nil;
    if ([value isKindOfClass:[NSImage class]]) {
        d = [NSKeyedArchiver archivedDataWithRootObject:value];
    }
    return d;
}

@end

In AppController class you initialize the transformer:

+ (void)initialize {
    DataToImageTransformer *transformer = [[DataToImageTransformer alloc] init];
    [NSValueTransformer setValueTransformer:transformer forName:@"DataToImageTransformer"];
    [transformer release];
}

Then in Interface Builder you put "DataToImageTransformer" in for the binding. Now you have control over the binding and can do as I explained earlier in the transformer class. Note that I'm using NSKeyedArchiver to convert an NSImage to data and back again but you can use tiffRepresentation or any other method you want in its place.

这篇关于核心数据图像将不会加载到NSTableView图像单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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