拖放 - 是否可以获取URL? [英] Drag and Drop - Is it possible to get the URL?
问题描述
我实现了一个简单的拖放。主要目的是让用户从浏览器中拖放图像。我可以得到这个图像被拖动的URL吗?
I implemented a simple drag and drop. The main purpose is for users to drag and drop images from a browser. Is it possible for me get the URL that this image was dragged from?
所以说,我在SO和我拖放标志。有没有办法让我知道这是从 http://stackoverflow.com ?
感谢
So lets say, I am on SO and I drag and drop the logo. Is there a way for me to know that this was from http://stackoverflow.com ? Thanks
推荐答案
对于不是链接的图片,以下代码会记录一个拖动的图片从。这适用于我在Safari&火狐。
For images which aren't also links, the following code will log the URL a dragged image came from. This works for me in Safari & Firefox.
@implementation DragView
- (void)awakeFromNib {
[self registerForDraggedTypes:[NSArray arrayWithObject:NSURLPboardType]];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
return NSDragOperationCopy;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender {
NSPasteboard *pboard;
pboard = [sender draggingPasteboard];
NSLog(@"types: %@", [pboard types]);
NSLog(@"url: %@", [NSURL URLFromPasteboard:pboard]);
return YES;
}
@end
如果图像也是链接,则记录的URL是该链接的href。还可以使用扩展属性的kMDItemWhereFroms键从文件中获取从哪里来的URL(如Finder的获取信息面板中所示)。
If the image is also a link, the logged URL is a href of that link. It's also possible to get the "where from" URL from a file (as seen in the Finder's Get Info panel) using the kMDItemWhereFroms key of the extended attributes.
这篇关于拖放 - 是否可以获取URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!