Using Cocoa to create an icon for a folder

后端 未结 1 1445
遇见更好的自我
遇见更好的自我 2020-12-15 14:49

In my Mac OS application, I\'m prompting a user to create a new folder. I would like to apply an icon to this folder using Cocoa when it is created. Currently, to create the

相关标签:
1条回答
  • 2020-12-15 15:07

    The NSWorkspace method works like a charm here. Maybe your icon is in an invalid format?
    I tried setIcon: using the Finder icon:

    - (IBAction)setFolderIcon:(id)sender
    {
        NSOpenPanel* openPanel = [NSOpenPanel openPanel];
        [openPanel setCanChooseFiles:NO];
        [openPanel setCanChooseDirectories:YES];
        switch([openPanel runModal])
        {
            case NSFileHandlingPanelOKButton:
            {
                NSURL* directoryURL = [openPanel directoryURL];
                NSImage* iconImage = [[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns"];
                BOOL didSetIcon = [[NSWorkspace sharedWorkspace] setIcon:iconImage forFile:[directoryURL path] options:0];
                NSLog(@"%d", didSetIcon);
                [iconImage release];
            }
            case NSFileHandlingPanelCancelButton:
            {
                return;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题