1

I need to load an SVG from HTML data in Swift using the SwiftSVG framework.

The HTML data looks like this:

<svg height="600" version="1.1" width="600" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative;"><desc style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Created with Raphaƫl 2.0.0</desc><defs style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></defs>

<path fill="#ffffff" stroke="#000000" d="M157.5,82.5L157.5,127.5L202.5,127.5L202.5,82.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

<path fill="#ffffff" stroke="#000000" d="M7.5,232.5L7.5,277.5L52.5,277.5L52.5,232.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

<path fill="#ffd60a" stroke="#000000" d="M52.5,232.5L52.5,277.5L97.5,277.5L97.5,232.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

<path fill="#ffd60a" stroke="#000000" d="M97.5,232.5L97.5,277.5L142.5,277.5L142.5,232.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

<path fill="#ff453a" stroke="#000000" d="M7.5,277.5L7.5,322.5L52.5,322.5L52.5,277.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

<path fill="#ff9f0a" stroke="#000000" d="M247.5,472.5L247.5,517.5L292.5,517.5L292.5,472.5Z" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>

</svg>

And here's how this data is pulled and used in a WKWebView:

webView.evaluateJavaScript("newDiv.innerHTML") { (innerHTML, error) in

            dump(error)

            self.scrambleView.contentMode = .scaleAspectFit
            self.scrambleView.isOpaque = false
            self.scrambleView.backgroundColor = UIColor.clear
            self.scrambleView.scrollView.backgroundColor = UIColor.clear

            self.scrambleView.loadHTMLString(innerHTML as? String ?? " ", baseURL: nil)           

        }

The HTML data is stored in newDiv.innerHTML.

How do I use this as data in an SwiftSVG SVGView? And going beyond that, how would I be able to convert the resulting SVG to a UIImage for use in a UIImageView?

jcdr04
  • 90
  • 1
  • 8

0 Answers0