核心数据图像不会加载到 NSTableView 图像单元中

发布于 2024-09-02 18:23:45 字数 335 浏览 12 评论 0原文

在我的代码中,我将图像存储到我的核心数据模型中(工作正常)。如果我将视图设置为具有 NSImageView 并将其数据绑定到 Controller Key:selection 和 modelKeyPath:myImagePath,则它可以工作。它将显示所选行的每个图像。

然后,我在 NSTableView 中创建一个新列,并将图像单元格拖到该列上。但是,我无法让我的核心数据绑定将图像显示在单元格中。我尝试过绑定值和数据,但没有成功。

既然我确定图像已正确存储,那么我在绑定中做错了什么以防止图像显示在表格单元格中?

非常感谢。

(我的背景:新的 Cocoa 开发人员最近读完了整本书。)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谁把谁当真 2024-09-09 18:23:45

我不确定是什么原因导致了您的问题,但有一个简单的方法可以找出问题所在。将 NSValueTransformer 连接到绑定。然后在该变压器中,您可以记录一些内容以查明您是否传递了 nil 值,或者您可以将数据值转换为 NSImage 并将其传回……基本上您可以在变压器类中做任何您想做的事情。这是我在核心数据模型中的图像数据上使用的一个。

@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

在 AppController 类中,您初始化转换器:

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

然后在 Interface Builder 中,您将“DataToImageTransformer”放入绑定中。现在您可以控制绑定,并且可以按照我之前在变压器类中解释的那样进行。请注意,我使用 NSKeyedArchiver 将 NSImage 转换为数据并再次转换回来,但您可以使用 tiffRepresentation 或您想要的任何其他方法来代替它。

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.

通知家属抬走 2024-09-09 18:23:45

如果有人对 Xcode 6 的建议技术有疑问。我使用基于文档的应用程序创建了一个 Swift 项目。使用 ImageWell 控件 (NSImageView),绑定可以直接工作,无需变压器。
不过,有一个细节是,将您绑定到视图本身的数据绑定上,而不是绑定到其下的 ImageCell 对象上。

If anyone is having issues with the suggested technique with Xcode 6. I created a Swift project, using the Document based app. With an ImageWell control (NSImageView), bindings work directly without value the need of a transformer.
A detail though, put you binding on Data binding of the view itself, not on the ImageCell objet under it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文