skip to Main Content

I have a SVG Data downloaded from Internet in SVG format.

<svg viewBox="0 0 600 450" xmlns="http://www.w3.org/2000/svg"><rect x="0" y="0" width="600" height="450" fill="#E9F0FC"></rect><g transform="translate(0,410)" fill="none" font-size="10" font-family="sans-serif" text-anchor="middle" style="font-size: 8px;"><g class="tick" opacity="1" transform="translate(49.5,0)"><text fill="currentColor" ...

And I have a UIImageView where I would like to show picture from this data.

I was looking for some tutorials, but most of them are outdated or for objective-c.

I tried this

SVGImageView.image = UIImage(data: svgData)

But it says that I can’t convert String to Data

2

Answers


  1. You can use SVGKit for example.

    1. Install it using CocoaPods.

      pod 'SVGKit'
      
    2. Use it like this:

       let svgImage = SVGKImage(data: yourSVGData)
      
       testImage.image = svgImage.UIImage
      

    The framework also allows to init an SVGKImage from other different sources, for example it can download image for you when you provide it with URL.

    Login or Signup to reply.
  2. Until Xcode 13.x you couldn’t load SVG images natively. So the solution is using a third party like SVGKit. Using Xcode 14.x you can use Image("name") on SwiftUI. But, IMO, no good support yet. Frame view modifier doesn’t apply over a SVG Graph…

    https://github.com/allangarcia/SVG-Image

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search