2012-11-02 25 views
0

我創建了一個應用程序,其中包含一個ImageView子類,它可以直接從Finder拖放文件/文件夾。接受從iPhoto或Aperture拖放

事情是我現在試圖讓它接受來自iPhoto或Aperture的照片。

其中PboardType s我應該註冊嗎?

所有我目前做的是:

[self registerForDraggedTypes: 
    [NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; 

任何想法?

回答

3

使用粘貼板Peeker(來自Apple)告訴我,Aperture爲您提供文件名/ URL以及「光圈圖像數據」(不管是什麼)。 iPhoto似乎只會顯示「ImageDataListPboardType」,它是一個PLIST。我猜你可以看到NSLog()的結構,並從中提取圖像信息。它可能包含文件名/ URL信息以及實際圖像作爲數據。

+0

那麼,這是一個簡單而好的答案。而且信息豐富(我還沒有聽說過Pasteboard Peeker--儘管我想到應該有某種類似的工具在某個地方......)。非常感謝! ;-) –

+0

我已經鏈接到它,但我無法再找到示例代碼。這是一個Apple示例代碼項目。我仍然有我創建和保留的二進制文件,但我不確定技術上是否允許分發它。不過,還有第三方工具可以做同樣的事情。 –

0

你是正確的註冊NSFilenamesPboardType。要完成任務:

1:請確保您接受dragingEntered中的複製操作。通用操作不足。

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender { 

    NSDragOperation sourceDragMask = [sender draggingSourceOperationMask]; 
    NSPasteboard *pasteboard = [sender draggingPasteboard]; 
    if ([[pasteboard types] containsObject:NSFilenamesPboardType]) { 
      if (sourceDragMask & NSDragOperationCopy) { 
       return NSDragOperationCopy; 
      } 
    } 

    return NSDragOperationNone; 
} 

2:每張照片會有一個文件名。和他們一起做點什麼。

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { 
    NSPasteboard *pasteboard; 
    NSDragOperation sourceDragMask; 

    sourceDragMask = [sender draggingSourceOperationMask]; 
    pasteboard = [sender draggingPasteboard]; 

    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 (NSString* filename in filenames) 
      { 
       NSImage* image = [[NSImage alloc]initWithContentsOfFile:filename]; 
       //Do something with the image 
      } 
     } 
    } 

    return YES; 
}