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
iOS Swift Add SVG Image to button like Android
UPD: Also see this UseYourLoaf blog post
Just found on Erica Sadun blog post that on iOS 11 you could use Vector Assets.
What "Vector Assets" mean:
If you click that box, the vector data will be shipped with your
application. Which, on the one hand, makes your application a little
bit larger, because the vector data takes up some space. But on the
other hand, will give you the opportunity to scale these images, which
might be useful in a number of different situations. So, one is, if
you know that this particular image is going to be used at multiple
sizes. But that might be less obvious. So, one case is a symbolic
glyph that should resize with dynamic type. Since we're thinking about
dynamic type, you should also be thinking about having glyphs that are
appearing next to type resize appropriately. Another case that's
really not obvious, is tab bar images.
... there's a really great accessibility feature that we strongly
recommend supporting, that allows for user that have turned their
dynamic type size up. ... So, we really recommend doing that to increase the usability of your app across all users
How to use:
- Convert your SVG file into PDF, e.g. on ZamZar.com
- Add your pdf to
Assets.xcassets
- Click "Preserve Vector Data" for the imported pdf.
- Create
UIImageView
in yourUIViewController
and assign pdf file likeUIImage
.
or Asset Catalog Creator available in the Mac App Store will do steps 1 and 2 with a simple drag and drop.
iOS < 11
There is no native way to use SVG image.
Take a look at Macaw
Import framework via Cocoapod
pod "Macaw", "0.8.2"
Check their example project: this is how you render tiger.svg
(located in project directory, not in an Assets.xcassets
file)
import UIKit
import Macaw
class SVGExampleView: MacawView {
required init?(coder aDecoder: NSCoder) {
super.init(node: SVGParser.parse(path: "tiger"), coder: aDecoder)
}
}
There are some other third-party libraries of course:
- SwiftSVG
- Snowflake
- SVGKit Objective-C framework
Swift SVG Fil Color to SVGImage
Wish Swift adds this feature in their next release
After a nightmare, I came up with this solution for
Using SVG in Swift which fits automatically for the View and can add fill colors to it in just one line of code
This is for all who don't wish to struggle like me
|*| Usage :
let svgImgVar = getSvgImgFnc("svgImjFileName", ClrVar : ClrVar)
// Can use this Image anywhere in your code
|*| Steps :
I used the simple SwiftSVG library for getting UIView from SVG File
|*| Install SwiftSVG Library
1) Use pod to install :
// For Swift 3
pod 'SwiftSVG'
// For Swift 2.3
pod 'SwiftSVG', '1.1.5'
2) Add framework
Goto AppSettings
-> General Tab
-> Scroll down to Linked Frameworks and Libraries
-> Click on plus icon
-> Select SVG.framework
3) Add below code anywhere in your project
func getSvgImgFnc(svgImjFileNameVar: String, ClrVar: UIColor) -> UIImage
{
let svgURL = NSBundle.mainBundle().URLForResource(svgImjFileNameVar, withExtension: "svg")
let svgVyuVar = UIView(SVGURL: svgURL!)
/* The width, height and viewPort are set to 100
Related Topics
Sorting Nsarray of Dictionaries by Value of a Key in the Dictionaries
How to Capitalize Each Word in a String Using Swift iOS
How to Set Cg_Context_Show_Backtrace Environmental Variable
How to Reset the Privacy Settings in iOS
Resize Uiimage to 200X200Pt/Px
Use and Access Existing SQLite Database on iOS
How to Fix Xcode "Dtassetproviderservice Could Not Start.." Error
How to Determine Which Apps Are Background and Which App Is Foreground on iOS by Application Id
What Is the Height of Iphone's Onscreen Keyboard
Xcode Is Looking for Core Data Entity Names with Dot; Not Compiling
Xcode Stuck at "Your Application Is Being Uploaded"
Attempt to Insert Non-Property List Object When Trying to Save a Custom Object in Swift 3
How to Opt Your iPad App Out of Multitasking on iOS 9
How to Draw a Transparent Uitoolbar or Uinavigationbar in iOS7
How to Obtain a Dynamic Table View Section Header Height Using Auto Layout
Uiview Animatewithduration Doesn't Animate Cornerradius Variation