How to display .svg image using swift

前端 未结 13 876
你的背包
你的背包 2020-11-30 00:37

I have a .svg image file I want to display in my project.

I tried using UIImageView, which works for the .png & .jpg image formats, but not for the .svg extensi

13条回答
  •  独厮守ぢ
    2020-11-30 00:49

    In case you want to use a WKWebView to load a .svg image that is coming from a URLRequest, you can simply achieve it like this:

    Swift 4

    if let request = URLRequest(url: urlString), let svgString = try? String(contentsOf: request) {
      wkWebView.loadHTMLString(svgString, baseURL: request)
    }
    

    It's much simpler than the other ways of doing it, and you can also persist your .svg string somewhere to load it later, even offline if you need to.

提交回复
热议问题