简体   繁体   中英

How to bind NSImageView by path?

I have a program with tableview/details of core data object. 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). 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...

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

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 . 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. 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. Here's the main method for that functionality (after inheriting from 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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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