This approach does not work for me and I can't figure out how to do it.
extension Image {
func getResizedImage(_ imageName: String) -> any View {
return Image(imageName)
.resizable()
}
}
// then ...
struct ContentView: View {
var body: some View {
Image.getResizedImage("myImageFromAssets")
}
} ```
CodePudding user response:
Two issues:
Because you're calling this on
Image
, you need astatic
functionThe function should return
some View
, notany View
extension Image {
static func getResizedImage(_ imageName: String) -> some View {
return Image(imageName)
.resizable()
}
}
struct ContentView: View {
var body: some View {
Image.getResizedImage("myImageFromAssets")
}
}