How to Use .Svg Images in Swiftui

Uploading svg images to SwiftUI

there are two ways to implement SVG images in your project one is via using a library on GitHub called SVGKit (this way is expensive using the ram and slows down the loading time a little), but the other way that was perfect is using:

two libraries:
1- SDWebImageSVGCoder
2- SDWebImageSwiftUI

after you add these libraries to your project through the Xcode package dependencies, you write this code to set it up and use it.



import SwiftUI
import SDWebImageSVGCoder

@main
struct Package_trialsApp: App {


init() {
setUpDependencies() // Initialize SVGCoder
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}

// Initialize SVGCoder
private extension Package_trialsApp {

func setUpDependencies() {
SDImageCodersManager.shared.addCoder(SDImageSVGCoder.shared)
}
}



after setting that up you can use it like this:

import SDWebImageSwiftUI

WebImage(url: "https://www.svgrepo.com/show/423378/car-service.svg", options: [], context: [.imageThumbnailPixelSize : CGSize.zero])
.placeholder {ProgressView()}
.resizable()
.frame(width: 300, height: 300)

Bonus:
you can also do something like this to make your code more efficient and more reusable:


import SwiftUI

import SDWebImageSwiftUI


struct ContentView: View {

let imageArray = [
URL(string: "https://www.svgrepo.com/show/423378/car-service.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svgfiles/androi.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg"),
URL(string: "https://jpeg.org/images/jpeg-home.jpg")
]
var body: some View {
ScrollView {

Link(destination: URL(string: "https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/android.svg")!, label: {
Text("Sample SVG image")
.foregroundColor(.orange)
})

ForEach(imageArray, id: \.self) { url in
if let bolean = url?.absoluteString.suffix(3).localizedCaseInsensitiveContains("svg") {
if bolean {
HStack {
WebImage(url: url, options: [], context: [.imageThumbnailPixelSize : CGSize.zero])
.placeholder {ProgressView()}
.resizable()
.frame(width: 300, height: 300)
}
} else {
AsyncImage(url: url, content: { phase in
switch phase {
case .empty:
ProgressView()
case .failure:
Color.red
case .success(let image):
image.resizable().frame(width: 100, height: 100)


@unknown default:
fatalError()
}

})
}
}
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}


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:

  1. Convert your SVG file into PDF, e.g. on ZamZar.com
  2. Add your pdf to Assets.xcassets

    • Click "Preserve Vector Data" for the imported pdf.
  3. Create UIImageView in your UIViewController and assign pdf file like UIImage.

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


Related Topics



Leave a reply



Submit