2016-07-23 51 views
0

我正试图从Safari拖动的NSPromise图像获取NsUrl/NSData。我在哪里得到名称的NSURLOfPromisedFilesDroppedAtDestination

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

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

任何人都可以帮助我了解哪里可以从我的NSView下面的代码片段中获取dropLocation?

链接到文件: https://developer.apple.com/library/mac/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; 
} 

与得到上面的代码的工作版本任何帮助吗?

+1

'dropLocation'是一个变量,你创建的URL。你可以告诉拖放源在哪里写文件。 'namesOfPromisedFilesDroppedAtDestination'的文档说'设置承诺文件的放置位置,并返回接收器承诺在那里创建的文件的名称。' – Willeke

+0

这似乎只是返回Unknown Jpg,当我尝试将该NSURL转换为数据时,要上传 - 它只是返回为零,这似乎表明没有任何内容。 –

+0

这个答案可能会有所帮助:[我的OS X应用程序如何接受来自Photos.app?的图片文件的拖放(http://stackoverflow.com/a/30110526/4244136)。或者不使用承诺的文件,Safari也提供其他类型的文件。 – Willeke

回答

0

我能够得到它的工作感谢意见的帮助。

这里是为我工作的代码:

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) 

      } 
     }