Skip to content

Unable to scale SVG in SwiftUI #808

Open

Description

@OskarPersson

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?

Activity

  1. pauarmada commented on Jul 3, 2025

    @pauarmada
    Contributor

    @OskarPersson , I sent a PR for this to update the README: #833

    This is what we ended up using:

    extension Image {
        init(resource: KeyPath<MR.images, Shared.ImageResource>) {
            let imageResource = MR.images()[keyPath: resource]
            self.init(imageResource.assetImageName, bundle: imageResource.bundle)
        }
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions