Search code examples
iosswiftuiwebview

How to display .svg image using swift


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 extension. Is there any way to display a .svg image using UIWebView or UIImageView ?


Solution

  • Try this code

    var path: String = NSBundle.mainBundle().pathForResource("nameOfFile", ofType: "svg")!
    
            var url: NSURL = NSURL.fileURLWithPath(path)  //Creating a URL which points towards our path
    
           //Creating a page request which will load our URL (Which points to our path)
            var request: NSURLRequest = NSURLRequest(URL: url)
           webView.loadRequest(request)  //Telling our webView to load our above request