【问题标题】:Can you get full paths from the pasteboard when drag and dropping?拖放时可以从粘贴板中获取完整路径吗?
【发布时间】:2010-12-06 01:28:28
【问题描述】:

我支持 IKImageBrowserView 上的拖放操作。在我的拖放目标委托中,我接受 NSFilenamesPboardType 丢弃。奇怪的是,我返回的文件路径字符串都以“/Users/...”而不是“file://localhost/...”开头,就像我在使用 NSOpenPanel 时得到的那样。当我将这些转换为 NSURL 以供 IKImageBrowserView 使用以显示图像时,它会感到困惑,因为它只理解以“file://localhost/...”开头的完整路径。我可以破解前缀,就像我在下面的 sn-p 中所做的那样,但我想知道是否有一种编程方式来获取完整路径?

if ([[pasteboard types] containsObject:NSFilenamesPboardType])
{
    NSData* data = [pasteboard dataForType:NSFilenamesPboardType];
    if (data)
    {
        NSString* errorDescription;
        NSArray* filenames = [NSPropertyListSerialization
            propertyListFromData:data
            mutabilityOption:kCFPropertyListImmutable
            format:nil
            errorDescription:&errorDescription];

        for (id filename in filenames)
        {   
            NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"file://localhost%@", filename]];

            NSLog(@"Adding URL: %@", url);

【问题讨论】:

    标签: objective-c cocoa drag-and-drop


    【解决方案1】:

    要从完整路径创建文件 URL,请不要自己添加 file:// 前缀。只需使用

        NSURL* url=[NSURL fileURLWithPath:pathString];
    

    the documentation

    从操作系统的角度来看,

       /Users/myname/file.txt
    

    是系统中文件的完整路径。在也允许各种网络访问的更高级别的API中,本地文件通过放置file://协议来区分。 这些 pboard API 早于 API 中 URL 的普遍使用,这就是为什么它将路径作为完整路径返回,而不是作为文件 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2014-12-08
      • 1970-01-01
      • 2020-08-14
      相关资源
      最近更新 更多