I have a collection of folder and subfolders with images at the end of them. I tried displaying the image with something like
Image("folder/subFolder1/subFolder2/image.png")
All the images are added in Asset lib.
I do not see anything. Is this the wrong way to load and store images. If yes, please let me know what would be the best way here. The images are expected to be persistent and not change for a long time.
CodePudding user response:
So let me show you some ways that makes you free to typing all that folders after each other!
struct ContentView: View {
let urlOfImage = Bundle.main.url(forResource: "swiftPunk", withExtension: "png")
var body: some View {
// loading from Bundle:
if let unwrappedURL: URL = urlOfImage {
if let unwrappedUIImage: UIImage = UIImage(contentsOfFile: unwrappedURL.path) {
Image(uiImage: unwrappedUIImage)
.resizable()
.scaledToFit()
.frame(width: 300, height: 300)
.cornerRadius(10.0)
}
}
Text("Hello, world!").bold()
// loading from Assets:
Image("swiftPunk")
.resizable()
.scaledToFit()
.frame(width: 300, height: 300)
.cornerRadius(10.0)
}
}
So you need to use just this code:
Image("image")