Macos NSWorkspace设置图标未更新El Capitan上的应用程序图标
我正在使用NSWorkSpaceMacos NSWorkspace设置图标未更新El Capitan上的应用程序图标,macos,osx-elcapitan,Macos,Osx Elcapitan,我正在使用NSWorkSpaceSetIcon:forFile:options:更改应用程序图标。在约塞米蒂,它运行良好。然而,El Capitan上的应用程序图标没有更新。您并不孤单。 尝试先设置零图标,然后再设置您的图标: [[NSWorkspace sharedWorkspace] setIcon:nil forFile:path options:0] [[NSWorkspace sharedWorkspace] setIcon:image forFile:path options:0];
SetIcon:forFile:options:
更改应用程序图标。在约塞米蒂,它运行良好。然而,El Capitan上的应用程序图标没有更新。您并不孤单。
尝试先设置零图标,然后再设置您的图标:
[[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];
});
}
你好@EdiZ。上述解决方案不起作用。有没有其他方法可以做到这一点?