【问题标题】:How to Drag&Drop images from browser to Cocoa app如何将 Drop 图像从浏览器拖放到 Cocoa 应用程序
【发布时间】:2014-02-06 00:16:04
【问题描述】:

我需要将图像从浏览器拖到 mac 应用程序。从img标签拖动图像时,它可以正常工作,但是当img之间有链接时,它就不起作用了。 NSPasteboard 从链接接收 NSURL,而不是从 img 源。拖动反馈显示图像,所以我认为它正在发送图像数据。我从 NSPasteboard 中检查了 propertyListForType,它只包含链接 href。

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    if ([sender draggingSource] == nil){
        NSPasteboard *pboard = [sender draggingPasteboard]; 
        if ([[pboard types] containsObject:NSURLPboardType]){
            NSURL* file = [NSURL URLFromPasteboard: [sender draggingPasteboard]];
            //if <img> tag, file is src. If <a><img></a>, it’s link href

            //DOWNLOAD image here
        }
    }
}    

【问题讨论】:

  • 你想要 URL 还是只对图片感到满意?
  • 如果我有图片我也会很高兴 :) 但我需要知道文件扩展名,而不仅仅是 TIFF 表示

标签: objective-c macos cocoa nspasteboard


【解决方案1】:

NSPasteboard 上应该提供多种类型。每当我编写任何粘贴板接收代码时,我做的第一件事就是编写一些调试代码,这些代码会转储所有可用类型及其值,当拖动发生在我的视图中时。然后我就挑选我想要的。

作为开始,只是

NSLog(@“types %@“, pboard.types);

我将把它作为转储值的练习。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多