.Svg Url to Uiimage iOS

How to display .svg image using swift

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

unable to get image from URL to update in UIImage in swift

I tried installing SVGKit as given in the below reference link and it works for me.
How to display .svg image using swift

DispatchQueue.global(qos: .background).async { [weak self] () -> Void in
if let url = NSURL(string: "https://restcountries.eu/data/est.svg"){
if let data = NSData(contentsOf: url as URL) {
let imageAux = UIImage(data: data as Data)

DispatchQueue.main.async {
let anSVGImage: SVGKImage = SVGKImage(data: data as Data)
cell.imgFlag.image = nSVGImage.uiImage
}
}
}
}

in ur existing code just add one line of code:
let anSVGImage: SVGKImage = SVGKImage(data: data as Data) and it worked at my end.

Why can't I convert the `svg` data to UIImage using `SVGKit` in Swift 4?

If you are image is using gradient effect then it will not work in SVGKit. Check whether your image is using any gradient effect.

Apple's rendering DOES NOT ALLOW US to render this image correctly
using SVGKFastImageView, because Apple's renderInContext method -
according to Apple's docs - ignores Apple's own masking layers. Until
Apple fixes this bug, you should use SVGKLayeredImageView for this
particular SVG file (or avoid using gradients)



Related Topics



Leave a reply



Submit