【问题标题】:NSWorkspace SetIcon not updating application icon on El CapitanNSWorkspace SetIcon 未更新 El Capitan 上的应用程序图标
【发布时间】:2015-11-09 11:41:50
【问题描述】:

我正在使用 NSWorkSpace SetIcon:forFile:options: 更改应用程序图标。在优胜美地它工作正常。但是在 El Capitan 上,应用程序图标没有更新。

【问题讨论】:

    标签: macos osx-elcapitan


    【解决方案1】:

    你并不孤单。 尝试先设置 nil 图标,然后设置你的图标:

    [[NSWorkspace sharedWorkspace] setIcon:nil forFile:path options:0]
    [[NSWorkspace sharedWorkspace] setIcon:image forFile:path options:0];
    

    【讨论】:

    • 你好@EdiZ。上述解决方案不起作用。还有其他方法可以实现吗?
    【解决方案2】:

    我在 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];
            });
        }
    

    【讨论】:

      猜你喜欢
      • 2016-06-24
      • 2016-01-16
      • 2015-12-28
      • 2016-06-29
      • 2016-11-09
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多