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()
}
}