Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Can't display image in SwiftUI

I'm trying to display my apps icon within my app and it's not working. It displays a blank space instead and I don't understand why this is happening.

I tried creating a new image (just a normal image, not an 'App Icon' image set) and have this code:

Image("AppIcon")
    .resizable()
    .aspectRatio(contentMode: .fit)
    .frame(width: 48)
    .cornerRadius(10)
    .overlay(
        RoundedRectangle(cornerRadius: 10)
            .stroke(Color.black.opacity(0.1), lineWidth: 1)
        )

For some strange reason it's not displaying that either. The image name is correct. It's showing a blank white box.

Answered by darkpaw in 844321022

Take off all the modifiers so you're left with just the Image("AppIcon"). Does it work now?

If not, then it's something in your image.

How about renaming it? What size is it? What format is it?, JPG, PNG?

Take off all the modifiers so you're left with just the Image("AppIcon"). Does it work now?

If not, then it's something in your image.

How about renaming it? What size is it? What format is it?, JPG, PNG?

You can get your icon file name from the CFBundleIcons from your app’s information property list file and display that in your SwiftUI view. For example:

struct ContentView: View {
    var body: some View {
        if let iconName = appIconName(),
           let image = UIImage(named: iconName) {
            Image(uiImage: image)
                .resizable()
                .scaledToFit()
        }
    }

    func appIconName() -> String? {
        guard
            let iconsDictionary = Bundle.main.infoDictionary?["CFBundleIcons"] as? [String: Any],
            let primaryIconsDictionary = iconsDictionary["CFBundlePrimaryIcon"] as? [String: Any],
            let iconFiles = primaryIconsDictionary["CFBundleIconFiles"] as? [String],
            let lastIcon = iconFiles.last
        else {
            return nil
        }
        return lastIcon
    }
}

Can't display image in SwiftUI
 
 
Q