gpt4 book ai didi

objective-c - 使用 Cocoa 为文件夹创建图标

转载 作者:搜寻专家 更新时间:2023-10-30 20:04:08 25 4
gpt4 key购买 nike

在我的 Mac OS 应用程序中,我提示用户创建一个新文件夹。我想在创建时使用 Cocoa 将图标应用于此文件夹。目前,要创建文件夹,我使用以下代码:

- (IBAction)browseFiles:(id)sender
{
NSOpenPanel *oPanel = [[NSOpenPanel openPanel] retain];
[oPanel setCanChooseDirectories:YES];
[oPanel setCanChooseFiles:NO];
[oPanel setDelegate:self];
[oPanel setCanCreateDirectories:YES];
[oPanel beginSheetForDirectory:NSHomeDirectory()
file:nil
types:nil
modalForWindow:nil
modalDelegate:self
didEndSelector:@selector(filePanelDidEnd:
returnCode:
contextInfo:)
contextInfo:nil];
}

选择目录后,用户单击确认按钮,调用以下方法的函数:

bool set = [[NSWorkspace sharedWorkspace] setIcon:[NSImage imageNamed:@"icon.icns"] forFile:path options:NSExcludeQuickDrawElementsIconCreationOption]; 

虽然上面的代码确实返回"is",但图标并未成功应用于文件夹。我在我的代码中做错了什么吗?

谢谢。

最佳答案

NSWorkspace 方法在这里很有魅力。也许您的图标格式无效?
我尝试了 setIcon: 使用 Finder 图标:

- (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;
}
}
}

关于objective-c - 使用 Cocoa 为文件夹创建图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5739330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com