iosswiftsvgswiftuiswift-package

How can I display SVG bundled inside a Swift Package?


I am trying to display an image bundle inside a Swift Package.

This code let image = UIImage(named: "image", in: Bundle.module, compatibleWith: nil) works when I use png image. But when I use a svg with the same name (image.svg), the image is nil.

This is how I define the resource folder where I save the image in Package.swift

resources: [
  .process("Resources"),
]

Solution

  • Your file or asset can be in a xcassets, add a xcassets to Swift Package then import your asset there then type your resources like this:

    resources: [Resource.process("Media.xcassets")]
    

    Which Media.xcassets carry your asset, then use this code in Swift Package:

    Image("image", bundle: .module)