从iPhoto或Aperture接受拖放

时间:2012-11-02 14:47:39

标签: objective-c macos cocoa nspasteboard iphoto

我创建了一个应用程序,其中包含一个ImageView子类,可直接从Finder接受拖放文件/文件夹。

问题是我现在正试图让它接受来自iPhoto或Aperture的照片。

我应该注册哪个PboardType

我目前所做的只是:

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

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

使用Pasteboard Peeker(来自Apple)向我展示Aperture为您提供文件名/ URL以及“光圈图像数据”(无论是什么)。 iPhoto似乎只显示“ImageDataListPboardType”,这是一个PLIST。我猜你可以看看NSLog()看看它的结构并从中提取图像信息。它可能包括文件名/ URL信息以及实际图像作为数据。

答案 1 :(得分:0)

注册NSFilenamesPboardType是正确的。完成任务:

1:确保您接受draggingEntered中的复制操作。通用操作不足。

- (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;
}