iOS / macOS-绘制易于被系统污染的图像/图标的最佳方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS / macOS-绘制易于被系统污染的图像/图标的最佳方法相关的知识,希望对你有一定的参考价值。

创建一组受操作系统影响以显示彩色/重音符号的图标的最佳方法是什么?

  • 所选版本
  • 口音版本

我有大约10个黑色版本,但希望使用选定的或系统选定的强调色来调整它们。我应该如何处理我的图标,以使其由操作系统着色]

任何路线图?

答案

此代码显示了如何添加基于ios主题的替代图标以及您的设置包。我还添加了accent1Icon和accent2Icon作为示例。

在您的Settings.bundle中,您需要向“首选项”中添加“多值-应用”项(Apple要求用户可以更改主题,即使您根据其iOS设置自动对其进行设置):

<dict>
    <key>Type</key>
    <string>PSMultiValueSpecifier</string>
    <key>Values</key>
    <array>
        <string>0</string>
        <string>1</string>
        <string>2</string>
        <string>3</string>
        <string>4</string>
    </array>
    <key>Title</key>
    <string>App Icon Theme</string>
    <key>Key</key>
    <string>app_icon_theme</string>
    <key>DefaultValue</key>
    <string>0</string>
    <key>Titles</key>
    <array>
        <string>Use iOS Theme</string>
        <string>Dark Theme</string>
        <string>Light Theme</string>
        <string>Accent1 Theme</string>
        <string>Accent2 Theme</string>
    </array>
</dict>

创建至少具有您的app_icon_theme设置的类:

class SettingsBundleHelper {
    struct SettingsBundleKeys {
        static let AppIconThemeKey = "app_icon_theme"
    }
}

将此添加到您的info.plist:

<dict>
    <key>CFBundleAlternateIcons</key>
    <dict>
        <key>darkIcon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>name-of-dark-icon</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>
        <key>accent1Icon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>name-of-accent1-icon</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>  
        <key>accent2Icon</key>
        <dict>
            <key>CFBundleIconFiles</key>
            <array>
                <string>name-of-accent2-icon</string>
            </array>
            <key>UIPrerenderedIcon</key>
            <false/>
        </dict>     
    </dict>
    <key>CFBundlePrimaryIcon</key>
    <dict>
        <key>CFBundleIconFiles</key>
        <array>
            <string>AppIcon</string>
        </array>
        <key>UIPrerenderedIcon</key>
        <false/>
    </dict>
</dict>

在您的项目中创建一个名为Alternate Icons的文件夹,然后在其中保存一个name-of-dark-icon@2x.png和name-of-dark-icon@3x.png。

YourProject 
|
+-- Alternate Icons (folder)
   |
   +-- name-of-dark-icon@2x.png
   +-- name-of-dark-icon@3x.png
   +-- name-of-accent1-icon@2x.png
   +-- name-of-accent1-icon@3x.png
   +-- name-of-accent2-icon@2x.png
   +-- name-of-accent2-icon@3x.png

您可以在AppDelegate applicationDidBecomeActive(或其他地方)中运行changeIcon代码。如果选择在此处运行,则需要将其添加到AppDelegate文件:

enum AppIconTheme: String {
   case UseiOSTheme = "0"
   case DarkTheme = "1"
   case LightTheme = "2"
   case Accent1Theme = "3"
   case Accent2Theme = "4"
}

func applicationDidBecomeActive(_ application: UIApplication) {
// update icon if dark mode
if #available(iOS 12.0, *) {
    var theme = AppIconTheme.UseiOSTheme
    if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
        if let themeSettings = AppIconTheme(rawValue: iconTheme) {
            theme = themeSettings
        }
    }
    print(theme as Any)
    switch (theme) {
        case .UseiOSTheme:
            if UIApplication.shared.windows[0].rootViewController?.traitCollection.userInterfaceStyle == .dark {
                self.changeIcon(to: "darkIcon")
            } else {
                self.changeIcon(to: nil)
            }
        case .LightTheme:
            self.changeIcon(to: nil)
        case .DarkTheme:
            self.changeIcon(to: "darkIcon")
        case .Accent1Theme:
            self.changeIcon(to: "accent1Icon")
        case .Accent2Theme:
            self.changeIcon(to: "accent2Icon")
        }
    } else {
        // Fallback on earlier versions
        var theme = AppIconTheme.UseiOSTheme
        if let iconTheme = UserDefaults.standard.string(forKey: SettingsBundleHelper.SettingsBundleKeys.AppIconThemeKey) {
            theme = AppIconTheme(rawValue: iconTheme)!
        }
        print(theme as Any)
        switch (theme) {
        case .UseiOSTheme:
            self.changeIcon(to: nil)
        case .LightTheme:
            self.changeIcon(to: nil)
        case .DarkTheme:
            self.changeIcon(to: "darkIcon")
        case .Accent1Theme:
            self.changeIcon(to: "accent1Icon")
        case .Accent2Theme:
            self.changeIcon(to: "accent2Icon")
        }
    }
}

func changeIcon(to name: String?) {
    //Check if the app supports alternating icons
    guard UIApplication.shared.supportsAlternateIcons else {
        return;
    }

    if UIApplication.shared.alternateIconName != name {
        //Change the icon to a specific image with given name
        // if name is nil, the app icon will be the default app icon
        UIApplication.shared.setAlternateIconName(name) { (error) in
             //After app icon changed, print our error or success message
             if let error = error {
                 print("App icon failed to due to (error.localizedDescription)")
             } else {
                 print("App icon changed successfully.")
             }
        }
    }
}

以下是Apple在setAlternativeIcon上的文档:https://developer.apple.com/documentation/uikit/uiapplication/2806818-setalternateiconname

以上是关于iOS / macOS-绘制易于被系统污染的图像/图标的最佳方法的主要内容,如果未能解决你的问题,请参考以下文章

有没有办法确定跨域图像是不是会在不绘制画布的情况下污染画布?

使用 Chrome 在画布上本地绘制图像

被 CORS 图像污染的画布

全功能iMazing macOS特别版+win系统特别版iOS设备管理+存档管理

「AdGuard」这款 macOS 广告拦截应用有点厉害,全局解决广告污染和隐私追踪

macOS && iOS系统结构