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
As I know there are 2 different graphic formats:
So if you need to use an image stored in SVG File in your Xcode I would suggest:
Convert SVG file to PDF. I used https://document.online-convert.com/convert/svg-to-pdf
Use Xcode to manage you PDF file.
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
You can use this pod called 'SVGParser'. https://cocoapods.org/pods/SVGParser.
After adding it in your pod file, all you have to do is to import this module to the class that you want to use it. You should show the SVG image in an ImageView.
There are three cases you can show this SVGimage:
You can also find an example project in GitHub: https://github.com/AndreyMomot/SVGParser. Just download the project and run it to see how it works.
To render SVG file you can use Macaw. Also Macaw supports transformations, user events, animation and various effects.
You can render SVG file with zero lines of code. For more info please check this article: Render SVG file with Macaw.
DISCLAIMER: I am affiliated with this project.
SVG file support is added to Xcode 12.
You can use SVGKit for example.
1) Integrate it according to instructions. Drag&dropping the .framework file is fast and easy.
2) Make sure you have an Objective-C to Swift bridge file bridging-header.h with import code in it:
#import <SVGKit/SVGKit.h>
#import <SVGKit/SVGKImage.h>
3) Use the framework like this, assuming that dataFromInternet is NSData, previously downloaded from network:
let anSVGImage: SVGKImage = SVGKImage(data: dataFromInternet)
myIUImageView.image = anSVGImage.UIImage
The framework also allows to init an SVGKImage from other different sources, for example it can download image for you when you provide it with URL. But in my case it was crashing in case of unreachable url, so it turned out to be better to manage networking by myself. More info on it here.