我有一个带有 3 个对象、两个 NSTextField 和一个 NSImageView 的 NSCollectionView。我通过让用户选择一个文件夹然后读取该文件夹的内容来获取我的数据。我提取文件名、路径和类型(文件/文件夹)的嗅探,如果它是一个文件夹,则获取它的图标(使用 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
有人可以指出我正确的方向来纠正这个...谢谢