I am attempting to load images from the a bundle in my project. Instead I get the following error:
2020-03-20 11:38:53.032321-0400 Journey[4626:1025739] [SwiftUI] No image named 'CountryPicker.bundle/Images/AF.png' found in asset catalog for main bundle (/private/var/containers/Bundle/Application/720B947B-209E-4527-AE1C-B73D905D3D35/Journey.app)
As you can see in the screenshot, the image is in the bundle:
Here is my code:
Picker(selection: $countryOrigin, label: Text("Country of Origin")) {
Section(header: SearchBar(text: $fetcher.searchQuery)) {
List(fetcher.country) { country in
HStack() {
Image("CountryPicker.bundle/Images/\(country.id).png")
Text(country.name)
}
}
}
}
Does anyone know how to resolve this!?
UPDATE
If you move image to xcAssets, it works. You can also call Image(uiImage(named: "imageName.png")!)
In my testing, SwiftUI's Image
appears to only work with XCAsset catalogs. I had a use case where I couldn't use XCAsset catalogs easily, so I made this workaround View:
struct ImageWithoutCatalog: View, UIViewRepresentable {
var name: String
var bundle: Bundle
func makeUIView(context: Context) -> UIImageView {
UIImageView(image: UIImage(named: name, in: bundle, with: nil))
}
func updateUIView(_ uiView: UIImageView, context: Context) {
uiView.image = UIImage(named: name, in: bundle, with: nil)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.