1

我需要使用 SwiftSVG 框架从 Swift 中的 HTML 数据加载 SVG。

HTML 数据如下所示:

<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>

以下是如何在 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)           

        }

HTML 数据存储在newDiv.innerHTML.

如何将其用作 SwiftSVG SVGView 中的数据?除此之外,我如何能够将生成的 SVG 转换为 UIImage 以在 UIImageView 中使用?

4

0 回答 0