Search code examples
iossvgswiftuisvgkit

Swift: Unable to Load SVG with SVGKit - "Assertion failure in +[SVGLength pixelsPerInchForCurrentDevice]"


I’m trying to load an SVG image in a SwiftUI project using the SVGKit library, but the SVG fails to load, and I encounter the following error:

Assertion failure in +[SVGLength pixelsPerInchForCurrentDevice], SVGLength.m:238

I’ve tried two approaches to load the SVG file:

  1. Loading from the app bundle:
if let filePath = Bundle.main.path(forResource: "rat", ofType: "svg"),
   let svgImage = SVGKImage(contentsOfFile: filePath) {
    Image(uiImage: svgImage.uiImage)
        .resizable()
        .scaledToFit()
} else {
    Text("Failed to load SVG")
        .foregroundColor(.red)
}

  1. Loading using the SVGKImage(named:) method:
if let svgImage = SVGKImage(named: "rat") {
    Image(uiImage: svgImage.uiImage)
        .resizable()
        .scaledToFit()
} else {
    Text("Failed to load SVG")
        .foregroundColor(.red)
}

The Issue: Both approaches fail. The first method throws the error mentioned above, and the second method shows the warning:

[SVGKImage] WARNING: not caching the output for new SVG image with name = rat, because it failed to load correctly.

What I've Tried:

  1. Confirmed that the SVG file is in the correct directory and part of the app's bundle.
  2. Verified that the SVG file name matches exactly, including its case.
  3. Used multiple SVG files (some simplified versions).
  4. Enabled SVGKit logging (SVGKEnableLogging()), but no additional insights.
  5. Tried with SVGs that work in other apps or online validators.

this is the SVG i am trying to display text

Questions: Is this error related to device-specific DPI settings, as hinted by the pixelsPerInchForCurrentDevice method? Does SVGKit require any special configuration to handle modern SVGs? Are there any known limitations with SVGKit for certain types of SVG files?


Solution

  • I don't know why but I changed library to load SVG than it's working this is new library I used https://github.com/exyte/SVGView than it's working but I don't know why not working in SVGKit as that is widely used if anyone know that answer it