NSCollection错误:无法从对象NSImage创建NSData

时间:2012-01-31 14:41:10

标签: objective-c nscollectionview

我有一个带有3个对象的NSCollectionView,两个NSTextField和一个NSImageView。我通过让用户选择一个文件夹然后读取该文件夹的内容来获取我的数据。我拉文件名,路径和嗅探类型(文件/文件夹),如果它是一个文件夹获取其图标(使用NSWorkspace)或如果它是一个文件的图像。 (这些应该都是图像文件,有一个过滤器可以检查)。

无论如何,我的绑定似乎是正确的,因为文本字段已经填充但NSImageView不是,当我尝试将NSImage传递给我的模型时,我收到此错误消息。

//loop through the data
for (NSString* thisItemKey in fileManagerResults) { 

    //get our item data dictionary
    NSDictionary* thisItemData = [fileManagerResults objectForKey:thisItemKey];
    //get the item name
    NSString* itemName = [thisItemData objectForKey:@"Item Name"];

    //filter for hidden files 
    NSString *value = [itemName substringWithRange:NSMakeRange(0, 1)];
    if(![value isEqualToString:@"."]) {

        //make a new imageModel
        ImageModel* thisImageModel = [[ImageModel alloc] init];

        //add some data to our image model
        thisImageModel.itemName = itemName; 
        thisImageModel.itemType = [thisItemData objectForKey:@"Item Type"];
        thisImageModel.itemPath = [thisItemData objectForKey:@"Item Path"];
        thisImageModel.creationDate = [thisItemData objectForKey:@"Creation Date"];

        //get the file icon
        NSImage* theItemIcon;
        NSString* itemType = [thisItemData objectForKey:@"Item Type"];
        if ([itemType isEqualToString:@"Directory"]) { 
            theItemIcon = [[NSWorkspace sharedWorkspace] iconForFile:[thisItemData objectForKey:@"Item Path"]];

        } else { 
            theItemIcon = [[NSImage alloc] initWithContentsOfFile:[thisItemData objectForKey:@"Item Path"]];
        }

        thisImageModel.itemIcon = theItemIcon; 

        [tempArray addObject:thisImageModel]; 



    }

    //pass the data to our controller
    [self setControllerArray:tempArray];


}

每当我运行此操作时,我会收到以下错误消息:

Cannot create NSData from object NSImage 0x16074e10 Size={450, 350} Reps=(
NSBitmapImageRep 0x16075ba0 Size={450, 350} ColorSpace=NSCalibratedRGBColorSpace BPS=8 BPP=24 Pixels=450x350 Alpha=NO Planar=NO Format=0
) of class NSImage

有人能指出我正确的方向来纠正这个......谢谢

1 个答案:

答案 0 :(得分:0)

解决了,我正在为我的绑定选择数据而不是IB中的值。是的,我非常羞愧地坐在这里。 :D