|
|
When loading by name, `SVGView` maintains an internal cache for efficient repeated lookups.
For more predictable performance (avoiding cache lookups or parsing), you can pass in an already-constructed `SVG` instance:
```swift
var image: SVG
var body: some View {
SVGView(svg: image)
}
```
Remote SVGs can be loaded using `AsyncSVGView`:
```swift
AsyncSVGView(url: URL(string: "https://example.com/image.svg"))
.resizable()
.scaledToFit()
```
For custom loading and error states, use the phase-based initializer:
```swift
AsyncSVGView(url: url) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let svg):
SVGView(svg: svg)
case .failure:
Image(systemName: "exclamationmark.triangle")
}
}
```
### Loading and Transforming SVGs
SVG images can be loaded and rasterized to `UIImage` or `NSImage`:
```swift
let svg = SVG(named: "sample.svg", in: .main)!
imageView.image = svg.rasterize()
```
Transformations can be added before rasterizing:
```swift
let svg = SVG(named: "fish.svg")! // 100x100
.expanded(left: 10, right: 10) // 120x100
.scaled(2) // 240x200
imageView.image = svg.rasterize() // 240x200
```
### UIKit
Create a `UIImage` directly from an SVG within a bundle, `Data` or file `URL`:
```swift
import SwiftDraw
let image = UIImage(svgNamed: "sample.svg")
```
### AppKit
Create an `NSImage` directly from an SVG within a bundle, `Data` or file `URL`:
```swift
import SwiftDraw
let image = NSImage(svgNamed: "sample.svg")
```
## Command line tool
The command line tool converts SVGs to other formats: PNG, JPEG, SFSymbol and Swift source code.
```
copyright (c) 2026 Simon Whitty
usage: swiftdraw