Heejae Kim
Heejae Kim

Reputation: 539

SwiftUI No image named was found in main bundle

I'm studying SwiftUI and have a problem with Image(name: String).

This is my ContentView

var body: some View {
    VStack {
        Image("test")
            .resizable()
            .aspectRatio(contentMode: .fit)
            .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 75, alignment: .center)
            .clipped()
        List(retriever.items) { item in
            Row(coin: item)
        }
    }
}

When I ran this project, I got this message "Thread 1: Fatal error: No image named test was found in main bundle"

So, I tried below.

But, the result was same.

That image was in this path 'project root/images/test.jpg'


Strange thing is that this code works well.

Image(uiImage: UIImage(named: "test.jpg")!)

How can I solve this problem?

Upvotes: 20

Views: 24789

Answers (13)

Fattie
Fattie

Reputation: 12621

The solution seems to be that:

1. If you have the image in an Asset Catalog ...

then the code Image("small.icon") works fine.

2. If you have the image simply in the target ...

So, you have the image added to the target, just like any other say code file,

then you must use Image(uiImage: UIImage(named: "some.icon")!)

Hence simply drag the image from your desktop to an Asset Catalog, or, just use the other code format.

That seems to be the size of it. Basically they seem to "want" us to use Asset Catalogs now in the SwiftUI era and that's that.

Upvotes: 0

harrygg
harrygg

Reputation: 662

None of the answers helped. The image was part of the assets, in the correct directory, in the Json file etc. Once I restarted XCode the image showed up.

Upvotes: 0

Marcello Chuahy
Marcello Chuahy

Reputation: 1

Try specifying the bundle:

let bundle = Bundle.main
Image("test", bundle: bundle)

Upvotes: 0

Peter Chan
Peter Chan

Reputation: 1

You need to name your images [email protected] and [email protected] and [email protected], Then create New image Set in the Assets directory of the xcode project, rename it, and drag the image of the corresponding size to it. enter image description here

Upvotes: -2

user12410581
user12410581

Reputation:

In my case, I had the assets catalog inside a swift package, I used this code

Image("header", bundle: .module)

Upvotes: 2

Marcio Ferrini
Marcio Ferrini

Reputation: 1

I was able to solve my issue based on Monica's answer. It endup that my Assets.xcassets was not included in the Bundle resources.

Upvotes: 0

Asesh
Asesh

Reputation: 3361

Came across this thread from Google. None of the answers worked for me. I had dropped that PNG file into the project and this worked:

if let image_url = Bundle.main.url(forResource: "selfie", withExtension: "png") {
    Image(uiImage: UIImage(imageLiteralResourceName: image_url.lastPathComponent)!)
        .renderingMode(.original)
        .resizable().scaledToFit()
}

I had to use the overloaded version of Image(uiImage: UIImage(imageLiteralResourceName:)). Just specify the file name of that image only, using absolute path of the image file won't work

Upvotes: 1

Monica Granbois
Monica Granbois

Reputation: 7202

Also check that the Assets.xcassets is included to be copied in the Bundle Resources. You can find this by going to Targets -> Build Phases -> Copy Bundle Resources

Image showing the Build Phases menu, with expanded Copy Bundle Resources section

Upvotes: 1

joe
joe

Reputation: 11

I may be wrong but I think swift defaults to .png files. Perhaps when you simply write Image("test") it is looking for "test.png"

Try saving your image as a .png, placing it in the assests folder, and using Image("test") like you have been using above.

Upvotes: 1

Cody
Cody

Reputation: 1879

You have to add your images to the Assets Catalog in your Xcode project structure so Apple can do some behind the scenes magic to prep them for use with multiple screen sizes. Double click your Assets.xcassets folder (make an assets folder if you don't have one) in your project pane on the left of your xcode editor, then drag and drop your image file into the catalog pane.

Calls like Image("your_image_name") should work after this.

Upvotes: 7

Vinoth
Vinoth

Reputation: 9734

Please check whether your imageset's Contents.json contains the same name of image name. Please see the below image.

ImageSet of Conference Room

This worked for me.

Upvotes: 1

Ioan Roxin
Ioan Roxin

Reputation: 1

Save your image in PDF format ("test.pdf") and drag it into the asset catalog’s editor => Image("test") will be ok ...

Upvotes: 0

Radagast
Radagast

Reputation: 318

You should put your image to Assets.xcassets folder. Error message telling you literally - 'I can't find this image at Assets.xcassets'.

Upvotes: 1

Related Questions