【发布时间】:2015-11-09 11:41:50
【问题描述】:
我正在使用 NSWorkSpace SetIcon:forFile:options: 更改应用程序图标。在优胜美地它工作正常。但是在 El Capitan 上,应用程序图标没有更新。
【问题讨论】:
标签: macos osx-elcapitan
我正在使用 NSWorkSpace SetIcon:forFile:options: 更改应用程序图标。在优胜美地它工作正常。但是在 El Capitan 上,应用程序图标没有更新。
【问题讨论】:
标签: macos osx-elcapitan
你并不孤单。 尝试先设置 nil 图标,然后设置你的图标:
[[NSWorkspace sharedWorkspace] setIcon:nil forFile:path options:0]
[[NSWorkspace sharedWorkspace] setIcon:image forFile:path options:0];
【讨论】:
我在 OSx 10.14.6 上遇到了同样的问题。我使用了在创建文件后延迟设置图标的解决方法。立即设置图标有时会起作用,尽管返回成功,有时却无法刷新。
auto nspath = [NSString stringWithUTF8String:path.c_str()];
auto nsdata = [contents dataUsingEncoding:NSUTF8StringEncoding];
[[NSFileManager defaultManager] removeItemAtPath:nspath error:nil];
auto ret = [[NSFileManager defaultManager] createFileAtPath:nspath contents:nsdata attributes:nil];
// hide the extension of the file after creation.
if (ret) {
[[NSFileManager defaultManager] setAttributes:@{NSFileExtensionHidden: @true} ofItemAtPath:nspath error:nil];
// set the icon, after a delay. Bug in OSx, the icon is not reflected sometimes despite YES return
// if it is set immediately after creating the file.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
auto img = [NSImage imageNamed:@"Icon"];
[[NSWorkspace sharedWorkspace] setIcon:img forFile:nspath options:0];
});
}
【讨论】: