NSCollection 错误:无法从对象 NSImage 创建 NSData
我有一个带有 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
有人可以指出我正确的方向来纠正此问题...谢谢
I have a NSCollectionView with 3 objects, two NSTextFields and one NSImageView. I am getting my data by letting the user select a folder and then reading the contents of that folder. I pull the file name, path and sniff for type (file/folder) and if it is a folder get its icon (using NSWorkspace) or if it is a file its image. (these should all be image files, there's a filter in place to check).
Anyway, my bindings seem to be right as the text fields are getting populated but the NSImageView is not and I get this error message when I try to pass the NSImage to my model.
//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];
}
Whenever I run this though I get the following error message:
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
Can someone point me in the right direction to correct this...thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了,我正在选择用于绑定的数据,而不是 IB 中的值。是的,我坐在这里感到非常羞愧。 :D
Solved, I was selecting data for my binding and not value in IB. Yes, I sit here with great shame. : D