moko-resources icon indicating copy to clipboard operation
moko-resources copied to clipboard

Unable to scale SVG in SwiftUI

Open OskarPersson opened this issue 8 months ago • 0 comments
trafficstars

How do I properly scale SVG images in SwiftUI?

I'm running kotlin-2-sample in the latest commit with the following ContentView.swift:

import SwiftUI
import shared

extension Image {
    init(resource: KeyPath<MR.images, shared.ImageResource>) {
        self.init(uiImage: MR.images()[keyPath: resource].toUIImage()!)
    }
}

struct ContentView: View {
    let greet = Greeting().getMR()

	var body: some View {
        Image(resource: \.car_black)
            .resizable()
            .aspectRatio(contentMode: .fit)
	}
}

struct ContentView_Previews: PreviewProvider {
	static var previews: some View {
		ContentView()
	}
}

This creates this blurry mess:

Image

If I manually add the same image to the .xcassets file and update the code:

import SwiftUI
import shared

extension Image {
    init(resource: KeyPath<MR.images, shared.ImageResource>) {
        self.init(uiImage: MR.images()[keyPath: resource].toUIImage()!)
    }
}

struct ContentView: View {
    let greet = Greeting().getMR()

	var body: some View {
        Image(resource: \.car_black)
            .resizable()
            .aspectRatio(contentMode: .fit)

        Image("car_black")
            .resizable()
            .aspectRatio(contentMode: .fit)
	}
}

struct ContentView_Previews: PreviewProvider {
	static var previews: some View {
		ContentView()
	}
}

I get this

Image

Am I missing something?

OskarPersson avatar Mar 14 '25 23:03 OskarPersson