我有这段代码...
guard let filePath = Bundle.main.path(forResource: "AppIcon", ofType: "png") else {
print("Image not found")
return nil
}
答案 0 :(得分:1)
尝试一下。希望对您有帮助。
extension Bundle {
public var icon: UIImage? {
if let icons = infoDictionary?["CFBundleIcons"] as? [String: Any],
let primaryIcon = icons["CFBundlePrimaryIcon"] as? [String: Any],
let iconFiles = primaryIcon["CFBundleIconFiles"] as? [String],
let lastIcon = iconFiles.last {
return UIImage(named: lastIcon)
}
return nil
}
}
您可以像这样在应用程序中使用它:
let imageView = UIImageView()
imageView.image = Bundle.main.icon