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

发布于 2024-12-29 19:05:46 字数 2103 浏览 1 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-01-05 19:05:46

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

Solved, I was selecting data for my binding and not value in IB. Yes, I sit here with great shame. : D

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文