how to use .svg images in SwiftUI

svg swiftui
swiftui image
svg to png
svgkit
how to use svg image in swift 4
svg to pdf
xcode 11 svg
get image from svg

Can I only upload .png files into the assets folder? I get an error for .svg files. I want to use it for the Image() component

You can use pdf's for vectors in the asset catalog (select single scale and preserve vector data in the inspector on the right). You cannot directly use SVG's in the asset catalog, but with Xcode 11 you actually can use SVG's for symbols. Apple has a detailed guide on how to make your own SVG symbols here.

Loading SVG smbol in swiftui image, Image("foo") works fine with bitmaps from the asset catalog, but trying to use Image("bar") with an SVG – which was generated with the SF Symbols app,  An Image is a late-binding token; the system resolves its actual value only when it’s about to use the image in a given environment.

You can use Macaw to display a SVG: https://github.com/exyte/Macaw

1) Add the Package via SPM to your project (the tag 0.9.5 actually doesn't work, you need to use the master branch)

2) Move your .svg-file to the project or create a new Data set in your Assets.xcassets and then add the svg file to this data set.

3) Use this code:

struct MyView: View {

    var svgName: String

    var body: some View {
        SVGImage(svgName: self.svgName)
            .frame(width: 50, height: 550)
    }
}
struct SVGImage: UIViewRepresentable {

    var svgName: String

    func makeUIView(context: Context) -> SVGView {
        let svgView = SVGView()
        svgView.backgroundColor = UIColor(white: 1.0, alpha: 0.0)   // otherwise the background is black
        svgView.fileName = self.svgName
        svgView.contentMode = .scaleToFill
        return svgView
    }

    func updateUIView(_ uiView: SVGView, context: Context) {

    }

}

SwiftUI and SVG - iOS Conf SG 2020, Create your own SVG engine. (Hah!) Use a library like SVGKit. Wrap a WKWebView and load it in there. I went the WKWebView route for now, and it works fine,  The image you get back is scalable and colorable, which means you can tint the image using the same foregroundColor() modifier you’ve already seen: Image(systemName: "cloud.heavyrain.fill") .foregroundColor(.red) And it also means you can ask SwiftUI to scale up the image to match whatever Dynamic Type text style it accompanies, if any:

I've created a simple tool, which converts SVG code into SwiftUI's Shape object. It is very limited for now (in particular it only supports straight lines and cubic curves, single path SVGs and shapes that are filled with one color), but you can still use it for simple situations.

Here's the link to the tool, and the repository, in case you'd want to contribute and make it support all SVG features.

Creating Image in swiftUI with url for SVG image - swift, SF Symbols is a set of over 1500 vector-based symbol images (icons and glyphs) Any symbol in the SF Symbols app can be exported as an SVG file. The code for inserting a standard SF Symbol using SwiftUI and adding  The above method gets the image to animate but I can't apply resizable modifier or set frame size. In my situation, the image is too large and I'm unable to control the size. Also, I would like to know how I can control this animation from the SwiftUI view. I mean the duration, curves, repeat, etc. Would be of great help, best.

Be Swift: Creating Custom Symbols for Xcode 11 SwiftUI – Champlin , If we want to use Core Image, SwiftUI's Image view is a great end point, of image types, including bitmaps (like PNG), vectors (like SVG), and  Adding Images on Swift Playgrounds for iPad. Now switch back to the Playgrounds project and delete all the code related to Text. And, then start writing the code to overlay the two images like this: To display an image using SwiftUI, you use an Image view. It’s a bit tricky to import the image into the Playgrounds project.

Integrating Core Image with SwiftUI, Display and interact with SVG Images on iOS / OS X, using native rendering (​CoreAnimation) - SVGKit/SVGKit. If we want to use Core Image, SwiftUI’s Image view is a great end point, but it’s not useful to use elsewhere. That is, if we want to create images dynamically, apply Core Image filters, save them to the user’s photo library, and so on, then SwiftUI’s images aren’t up to the job.

SVGKit/SVGKit: Display and interact with SVG Images on , Native support for image assets in iOS apps is for PNG and JPEG formats. This is what is supported in the iOS SDK by Apple. While third-party libraries may  The trick is you need to view them in a Storyboard, not a SwiftUI Canvas. Add a UIImageView to your storyboard, then press the dropdown arrow near Image. You'll see the entire list of images provided by Apple: Copy the name of an image, and then use it just like other answers mention above: Image(systemName: "square.and.arrow.up")

Comments
  • I would recommend, also, looking at PaintCode. It will allow to import SVG files and export them as code, which can be used to generate scalable images or in painting code based on your needs
  • I get an error. Apparently it doesn't fulfill the requirements, as the page tells me you linked. I downloaded this svg package.. I have no idea about Adobe Illustrator or whatever..