简体   繁体   English

如何通过路径绑定NSImageView?

[英]How to bind NSImageView by path?

I have a program with tableview/details of core data object. 我有一个带有tableview /核心数据对象细节的程序。 One of the attributes is an image (it only appears in the details). 属性之一是图像(仅显示在详细信息中)。 I think it's best to save just the path to the image file, rather than the image itself (it seems to me that core data files with images are much bigger than the images, even if there is little more than that...). 我认为最好只保存图像文件的路径,而不是图像本身(在我看来,包含图像的核心数据文件比图像大得多,即使仅此而已...)。

Since it is supposed that the user drags the image to the imagewell, I thought that it would be appropriate to bind the imagewell to the array controller (AC) using "Value path" (AC.selection.image). 由于假定用户将图像拖到图像阱上,因此我认为使用“值路径”(AC.selection.image)将图像阱绑定到阵列控制器(AC)是合适的。 However, this doesn't do anything (the imagewell accepts the image dragged there, but it keeps there when we change selection). 但是,这无能为力(图像阱接受拖动到此处的图像,但是当我们更改选择时它会保留在此处)。

It seems likely to me that I must implement some "Value Transformer", but not the ones which are available (NSUnarchiveFromData and NSKeyedUnarchiveFromData), because I tried those already... 在我看来,我必须实现一些“值转换器”,而不是可用的(NSUnarchiveFromData和NSKeyedUnarchiveFromData),因为我已经尝试过了。

Am I right in this supposition? 我猜对了吗? And if so, what value transformer would that be? 如果是这样,那将是什么价值转换器? Is it something that I'll have to define? 我需要定义吗? Or is this altogether impossible? 还是这完全不可能?

Maybe I should add that I'm still using OSX 10.6, and thus some hypothesis seem to be ruled out... 也许我应该补充一点,我仍在使用OSX 10.6,因此似乎排除了某些假设...

Thanks 谢谢

Yes. 是。 It is possible. 有可能的。

  1. select the image view 选择图像视图
  2. Bind it to your object controller. 将其绑定到您的对象控制器。 In that value path field, you have to set the path value as string property 在该值路径字段中,您必须将路径值设置为字符串属性

From my experience, the "value path" and "value URL" bindings only work one-way, as in, you can specify the image view's contents with them , but you can't extract the path/URL from a dragged image . 根据我的经验,“值路径”和“值URL”绑定仅是单向工作,例如, 您可以使用它们指定图像视图的内容 ,但是不能从拖动的图像中提取路径/ URL The documentation says that the "binding is read-only", which is more than a little ambiguous, but I believe this is what it refers to. 该文档说“绑定是只读的”,这有点含糊不清,但是我相信这就是它的含义。

I ended up just using a text box with a path inside, a "Choose…" button, and the image view as merely a preview. 最后,我只使用了一个文本框,其中包含一个路径,一个“ Choose…”按钮,并且图像视图仅作为预览。 If you really want the "Image Well" functionality, however, I'd recommend something like KSImageView (GitHub gist) that grabs the path out of the NSPasteboard and stores/rebroadcasts it. 但是,如果您真的想要“ Image Well”功能,我建议您使用类似KSImageView (GitHub gist)之类的方法,该方法将从 NSPasteboard获取路径并进行存储/重新广播。 Here's the main method for that functionality (after inheriting from NSImageView ): 这是该功能的主要方法(从NSImageView继承后):

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard = sender.draggingPasteboard;
    NSString *plist = [pboard stringForType:NSFilenamesPboardType];

    if (!plist) return;

    NSArray<NSString*> *files =
        [NSPropertyListSerialization propertyListWithData:[plist dataUsingEncoding:NSUTF8StringEncoding]
                                                  options:NSPropertyListImmutable
                                                   format:NULL
                                                    error:NULL];
    if (files.count == 0) return;

    NSDictionary *userInfo = @{@"imagePath": files[0]};

    [[NSNotificationCenter defaultCenter] postNotificationName:@"KSImageDroppedNotification"
                                                        object:self
                                                      userInfo:userInfo];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM