
Vector graphics are fantastic. They give us small, compressible files, crispy rendered pixels at any scale, and they're supported by many different kinds of software and frameworks.
Except iOS 😭, which doesn't support them out of the box. Today we'll try a library from Michael Choe called SwiftSVG which provides great support for parsing and rendering SVG files. Let's take a look.
SwiftSVG supports a ton of different ways of getting SVGs into our app.
CAShapeLayer(SVGURL: SVGURL)
UIView(SVGURL: SVGURL)
UIView(pathString: deathStarBlueprints)
CAShapeLayer(pathString: superSecretNewLogoIdea)
UIBezierPath(pathString: "M150 0 L75 200 L225 200 Z")
SwiftSVG also provides a custom UIView subclass we can use in Interface Builder that supports IBInspectable/IBDesignable.

More broadly, it's important to think about the reasons why we might want to use SVGs in our app. SVGs are essentially XML files (Open one up in a text editor, you'll see.)
They are instructions on how to draw a set of paths. The files are incredibly small.
Getting a good workflow going for creating them at-will can be tricky, but Sketch (eventually) makes it mostly painless.
Once we have this, we can build some awesome abstractions. Imagine a Swift struct with SVGName, color, and size that can produces a UIImage.
struct SVGImage {
let SVGName: String
let size: CGSize
let color: UIColor
func asImage() -> UIImage {
// Neato!
}
}
More info about SwiftSVG can be found at git.io/swiftsvg
Tweet
Share
on reddit