Reputation: 1
I'm using Macaw to parse and render an SVG file gotten from the server.
Here's the source code: https://pastebin.com/g9vUCpGX
How can I accomplish this task?
class ViewController: UIViewController{
@IBOutlet weak var profileBadge: SVGView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/couchdb.svg")!
if profileBadge != nil{
profileBadge.loadSVG(from: url)
}
}
}
extension SVGView {
func loadSVG(from url: URL) {
DispatchQueue.global().async {
guard let data = try? Data(contentsOf: url) else {
return
}
guard let svgString = String(data: data, encoding: .utf8) else {
return
}
let node = (try? SVGParser.parse(text: svgString)) ?? Group()
DispatchQueue.main.async {
print(node)
self.node = node
}
}
}
}
Upvotes: 0
Views: 458
Reputation: 1
Was able to figure out the issue. :) profileBadge should've been a UIView not UIImageView. And in the Identity Inspector, Class should've been SVGView and Module should've been Macaw.
Upvotes: 0
Reputation: 1039
You may use XIB or Storyboard.. It works using like below.
import UIKit
import Macaw
import SnapKit
class ViewController: UIViewController{
var profileBadge: SVGView = SVGView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(profileBadge)
profileBadge.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
let url = URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/couchdb.svg")!
if profileBadge != nil{
profileBadge.loadSVG(from: url)
}
}
}
extension SVGView {
func loadSVG(from url: URL) {
DispatchQueue.global().async {
guard let data = try? Data(contentsOf: url) else {
return
}
guard let svgString = String(data: data, encoding: .utf8) else {
return
}
let node = (try? SVGParser.parse(text: svgString)) ?? Group()
DispatchQueue.main.async {
print(node)
self.node = node
}
}
}
}
Upvotes: 1