Last active
August 17, 2020 16:45
-
-
Save vigorouscoding/4659678 to your computer and use it in GitHub Desktop.
NSImageView subclass to get the filename of the dropped image and to disable deleting and cutting the image. The class sends a "KSImageDroppedNotification" with the image filename in the userinfo dictionary. I got the idea from: http://www.cocoabuilder.com/archive/cocoa/121824-how-do-capture-the-filename-of-an-image-dropped-in-an-nsimageview.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface KSImageView : NSImageView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "KSImageView.h" | |
@implementation KSImageView | |
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { | |
BOOL acceptsDrag = [super performDragOperation:sender]; | |
if (acceptsDrag) { | |
NSPasteboard *pboard = [sender draggingPasteboard]; | |
NSString *plist = [pboard stringForType:NSFilenamesPboardType]; | |
if (plist) { | |
NSArray *files = [NSPropertyListSerialization propertyListFromData:[plist dataUsingEncoding:NSUTF8StringEncoding] | |
mutabilityOption:NSPropertyListImmutable | |
format:nil | |
errorDescription:nil]; | |
if ([files count] == 1) { | |
NSDictionary *userInfo = @{@"imageFileName" : [[files objectAtIndex: 0] lastPathComponent]}; | |
[[NSNotificationCenter defaultCenter] postNotificationName:@"KSImageDroppedNotification" | |
object:nil | |
userInfo:userInfo]; | |
} | |
} | |
} | |
return acceptsDrag; | |
} | |
- (void) delete:(id)sender { | |
} | |
- (void) cut:(id)sender { | |
} | |
@end |
Thanks for the swift version!
Hi - it looks like you are using the wrong method to push the notification as you aren't including the userInfo. Try use the method with with the userinfo overloader.
Great job- would be interesting to see if there was a way to detect if the image was pasted into using the menu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also works in
concludeDragOperation:
Here’s a quick Swift version:
Oh, and I found that I couldn’t manipulate the image in the same frame so if you want to do anything to it, wait one stack frame :)