Skip to content

Commit

Permalink
Added ability to instantiate from asset catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
mchoe committed Nov 28, 2017
1 parent 23230de commit 2fc0d95
Show file tree
Hide file tree
Showing 137 changed files with 2,504 additions and 2,509 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ self.addSubview(fistBump)
#### Output:
![Put it here!](https://raw.githubusercontent.com/mchoe/SwiftSVG/master/images/fistBump.png)

Or you can add an SVG file to your Asset Catalog. You should add it as a Data Asset like this:

![Add a new Data Asset in your Asset Catalog](https://raw.githubusercontent.com/mchoe/SwiftSVG/master/images/assetCatalog.png)

```swift
let cowboyHat = UIView(SVGNamed: "cowboyHat") // In the asset catalog as a Data Asset
self.addSubview(cowboyHat)
```

#### Output:
![New York City.](https://raw.githubusercontent.com/mchoe/SwiftSVG/master/images/cowboyHat.png)

You can also create a new UIView from a remote `URL`:

```swift
Expand Down
41 changes: 33 additions & 8 deletions SwiftSVG/SVG Extensions/UIView+SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,46 @@ public extension UIView {
```
let view = UIView(SVGNamed: "hawaiiFlowers")
```
- Parameter SVGNamed: The name of the SVG resource in the main bundle with an `.svg` extension.
- Parameter SVGNamed: The name of the SVG resource in the main bundle with an `.svg` extension or the name an asset in the main Asset Catalog as a Data Asset.
- Parameter parser: The optional parser to use to parse the SVG file
- Parameter completion: A required completion block to execute once the SVG has completed parsing. The passed `SVGLayer` will be added to this view's sublayers before executing the completion block
*/
public convenience init(SVGNamed: String, parser: SVGParser? = nil, completion: ((SVGLayer) -> ())? = nil) {
guard let svgURL = Bundle.main.url(forResource: SVGNamed, withExtension: "svg") else {
self.init()
return

var data: Data?
if #available(iOS 9.0, *) {
if let asset = NSDataAsset(name: SVGNamed) {
data = asset.data
} else if let svgURL = Bundle.main.url(forResource: SVGNamed, withExtension: "svg") {
do {
data = try Data(contentsOf: svgURL)
} catch {
self.init()
return
}
} else {
self.init()
return
}
} else {
guard let svgURL = Bundle.main.url(forResource: SVGNamed, withExtension: "svg") else {
self.init()
return
}
do {
data = try Data(contentsOf: svgURL)
} catch {
self.init()
return
}
}
do {
let data = try Data(contentsOf: svgURL)
self.init(SVGData: data, parser: parser, completion: completion)
} catch {

guard let unwrapped = data else {
self.init()
return
}

self.init(SVGData: unwrapped, parser: parser, completion: completion)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"data" : [
{
"idiom" : "universal",
"filename" : "cowboyHat.svg",
"universal-type-identifier" : "public.svg-image"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class GithubViewController: UIViewController {
let svgView = UIView(SVGNamed: "fistBump")
return svgView
}),
CellItem(render: { (cellSize) -> UIView in

// Simplest example of an SVG stored in the assets catalog as a Data Asset

let svgView = UIView(SVGNamed: "cowboyHat")
return svgView
}),
CellItem(render: { (cellSize) -> UIView in

// Parsing a single path string syncronously
Expand Down
6 changes: 2 additions & 4 deletions SwiftSVGExamples/SwiftSVGExamples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
380FED081F00171B00EDB255 /* openclipart-angry-coder.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FECFF1F00171B00EDB255 /* openclipart-angry-coder.svg */; };
380FED091F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Bicicletta.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED001F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Bicicletta.svg */; };
380FED0A1F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Camello.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED011F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Camello.svg */; };
380FED221F0036F400EDB255 /* rabbitcowboy.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED211F0036F400EDB255 /* rabbitcowboy.svg */; };
380FED4A1F02064900EDB255 /* hawaiiFlowers.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED491F02064900EDB255 /* hawaiiFlowers.svg */; };
380FED4E1F036E1F00EDB255 /* DrawingBoard.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED4B1F036E1F00EDB255 /* DrawingBoard.svg */; };
380FED5B1F06907600EDB255 /* johnny-automatic-open-mouth.svg in Resources */ = {isa = PBXBuildFile; fileRef = 380FED5A1F06907600EDB255 /* johnny-automatic-open-mouth.svg */; };
Expand Down Expand Up @@ -133,7 +132,6 @@
380FECFF1F00171B00EDB255 /* openclipart-angry-coder.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-angry-coder.svg"; sourceTree = "<group>"; };
380FED001F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Bicicletta.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-Anonymous_Architetto_--_Bicicletta.svg"; sourceTree = "<group>"; };
380FED011F00171B00EDB255 /* openclipart-Anonymous_Architetto_--_Camello.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-Anonymous_Architetto_--_Camello.svg"; sourceTree = "<group>"; };
380FED211F0036F400EDB255 /* rabbitcowboy.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = rabbitcowboy.svg; sourceTree = "<group>"; };
380FED491F02064900EDB255 /* hawaiiFlowers.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = hawaiiFlowers.svg; sourceTree = "<group>"; };
380FED4B1F036E1F00EDB255 /* DrawingBoard.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = DrawingBoard.svg; sourceTree = "<group>"; };
380FED5A1F06907600EDB255 /* johnny-automatic-open-mouth.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "johnny-automatic-open-mouth.svg"; sourceTree = "<group>"; };
Expand All @@ -146,6 +144,7 @@
3813ED4B1EFC26F60054ECBD /* openclipart-zanzara_architetto_fran_03r.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-zanzara_architetto_fran_03r.svg"; sourceTree = "<group>"; };
3813ED4D1EFC284E0054ECBD /* openclipart-tux.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-tux.svg"; sourceTree = "<group>"; };
381E99C21EFA0DC400839918 /* polygons.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = polygons.svg; sourceTree = "<group>"; };
3828E3601FCE1514009552DA /* rabbitcowboy.svg */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = rabbitcowboy.svg; sourceTree = "<group>"; };
3870903B1F0C32AE00F72660 /* error.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = error.svg; sourceTree = "<group>"; };
3876E95B1EFCB2E000F8C04E /* openclipart-1346768588.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "openclipart-1346768588.svg"; sourceTree = "<group>"; };
3876E95C1EFCB2E000F8C04E /* pizza.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = pizza.svg; sourceTree = "<group>"; };
Expand Down Expand Up @@ -308,6 +307,7 @@
8AC4DBE61CB37E1C00137DC9 /* Resources */ = {
isa = PBXGroup;
children = (
3828E3601FCE1514009552DA /* rabbitcowboy.svg */,
3807CCFA1F2AEF3A00E78314 /* tea.svg */,
3882153C1F12DEB000C192A3 /* George_Kerr_Anderson_Patent_1885_1.svg */,
3870903B1F0C32AE00F72660 /* error.svg */,
Expand All @@ -319,7 +319,6 @@
380FED5A1F06907600EDB255 /* johnny-automatic-open-mouth.svg */,
380FED4B1F036E1F00EDB255 /* DrawingBoard.svg */,
380FED491F02064900EDB255 /* hawaiiFlowers.svg */,
380FED211F0036F400EDB255 /* rabbitcowboy.svg */,
3876E9641EFCC16500F8C04E /* berlin.svg */,
380FECFB1F00171B00EDB255 /* openclipart-1290111299.svg */,
380FECFD1F00171B00EDB255 /* openclipart-1332611679.svg */,
Expand Down Expand Up @@ -468,7 +467,6 @@
380FED671F06951100EDB255 /* tiger.svg in Resources */,
3876E9651EFCC16500F8C04E /* berlin.svg in Resources */,
3807CCFB1F2AEF3A00E78314 /* tea.svg in Resources */,
380FED221F0036F400EDB255 /* rabbitcowboy.svg in Resources */,
380FED651F06946500EDB255 /* Dulcimer.svg in Resources */,
3882153D1F12DEB000C192A3 /* George_Kerr_Anderson_Patent_1885_1.svg in Resources */,
380FED5B1F06907600EDB255 /* johnny-automatic-open-mouth.svg in Resources */,
Expand Down
Loading

0 comments on commit 2fc0d95

Please sign in to comment.