【问题标题】:Where do I get the NSURL for namesOfPromisedFilesDroppedAtDestination我在哪里可以获得 namesOfPromisedFilesDroppedAtDestination 的 NSURL
【发布时间】:2016-07-23 15:03:27
【问题描述】:

我正在尝试获取从 Safari 拖动的 NSPromise 图像的 NsUrl / NSData。

Apple 的拖放文档似乎很不清楚。 它说我应该假设有一个名为 Droplocation 的属性,但没有说: - 这是什么 - 它来自哪里 - 以及如何获得它。

NSView 没有放置位置属性 - 因此在我们被要求假设它存在之前知道我们从哪里得到它会很有用。

谁能帮我弄清楚我在哪里可以从我的 NSView 获取下面 sn-p 中的 dropLocation?

文档链接: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/DragandDrop/Tasks/DraggingFiles.html

NSURL *dropLocation; // Assume this exists

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    NSPasteboard *pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSFilesPromisePboardType] ) {
        NSArray *filenames = [sender
                namesOfPromisedFilesDroppedAtDestination:dropLocation];
        // Perform operation using the files’ names, but without the
        // files actually existing yet
    }
    return YES;
}

对获得上述代码的工作版本有任何帮助吗?

【问题讨论】:

  • dropLocation 是一个变量,您可以创建 url。您告诉拖动源将文件写入何处。 namesOfPromisedFilesDroppedAtDestination 的文档说“为承诺的文件设置放置位置并返回接收者承诺在那里创建的文件的名称。”
  • 这似乎只是返回 Unknown Jpg,当我尝试将该 NSURL 转换为数据并上传时 - 它只是返回为 nil,这似乎表明那里什么都没有。
  • 这个答案可能会有所帮助:How can my OS X app accept drag-and-drop of picture files from Photos.app?。或者不要使用承诺的文件,Safari 也提供其他类型。
  • @Willeke 谢谢你——但做一些简单的事情似乎很复杂。苹果可以简化 api

标签: swift macos cocoa drag-and-drop


【解决方案1】:

多亏了 cmets 的帮助,我才能让它工作。

这是对我有用的代码:

if let pastetype = pasteboard.types {

            let tempDir = NSURL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)

            if pastetype.contains("NSPromiseContentsPboardType") {

                //NSPromise
            let destination = sender.namesOfPromisedFilesDroppedAtDestination(tempDir)

                let tempdestination = tempDir.URLByAppendingPathComponent(destination![0])
                let userInfo:[String:NSURL] = ["fileUrl":tempdestination]

                //I send the resulting file to my viewcontroller using notification. 
            NSNotificationCenter.defaultCenter().postNotificationName(imageDroppedNotification, object: self, userInfo: userInfo)

            }
        }

【讨论】:

    猜你喜欢
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2010-11-17
    • 2013-07-03
    相关资源
    最近更新 更多