私は 3 つのオブジェクト、2 つの NSTextFields と 1 つの 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
誰かがこれを修正するために正しい方向に私を向けることができます...ありがとう