[英]How do I include iOS App Icon image within the app itself?
I have standard iOS app, with a standard app icon contained in Assets.我有标准的 iOS 应用程序,资产中包含一个标准的应用程序图标。
I'd like to display the app icon within the app (using SwiftUI).我想在应用程序中显示应用程序图标(使用 SwiftUI)。 Note that I am not asking how to set the app icon, or change the icon dynamically.请注意,我不是在问如何设置应用程序图标,或动态更改图标。 I just want to show the app icon within the app's own Settings view.我只想在应用程序自己的设置视图中显示应用程序图标。
It would appear the App Icon asset should just be like any other, and I could include it using the following (note there is no space between App and Icon in the default icon naming),看起来 App Icon 资产应该和其他任何资产一样,我可以使用以下内容包含它(注意在默认图标命名中 App 和 Icon 之间没有空格),
Image("AppIcon")
I've also tried experimenting with,我也试过尝试,
Image("icon_60pt@3x.png") // Pick out a specific icon by filename
Image("icon_60pt@3x") // Maybe it assumes it's a .png
Image("icon_60pt") // Maybe it auto picks most appropriate resolution, like UIKit
...but none of these work. ...但这些都不起作用。
How do I include the apps own icon within the app, without duplicating it as a separate Image Set (which I have tried, and does work.)如何在应用程序中包含应用程序自己的图标,而不将其复制为单独的图像集(我已经尝试过,并且确实有效。)
Thanks.谢谢。
The following works if app icon is correctly set for used device (ie. iPhone icons for iPhone, etc.)如果为使用过的设备正确设置了应用程序图标(即 iPhone 的 iPhone 图标等),则以下操作有效
Note: sizes of app icons must match exactly!注意:应用图标的大小必须完全匹配!
Tested with Xcode 11.4用 Xcode 11.4 测试
Image(uiImage: UIImage(named: "AppIcon") ?? UIImage())
This works:这有效:
extension Bundle {
var iconFileName: String? {
guard let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
let iconFileName = iconFiles.last
else { return nil }
return iconFileName
}
}
struct AppIcon: View {
var body: some View {
Bundle.main.iconFileName
.flatMap { UIImage(named: $0) }
.map { Image(uiImage: $0) }
}
}
You can then use this in any view as just:然后,您可以在任何视图中使用它:
AppIcon()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.