├── .gitignore ├── README.MD ├── Package.swift ├── Package.resolved ├── LICENSE └── Sources └── SVGImageSwiftUI └── SVGImageSwiftUI.swift /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | xcuserdata/ 5 | DerivedData/ 6 | .swiftpm/configuration/registries.json 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | .netrc 9 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # SVGImageSwiftUI 2 | 3 | A SwiftUI wrapper over [SVGKit](https://github.com/SVGKit/SVGKit) to load SVG image over URL. 4 | ![image](https://i.ibb.co/Qjf9XYn/Simulator-Screenshot-Apple-Vision-Pro-2023-06-24-at-15-13-54.png) 5 | 6 | ## Features 7 | 8 | - Plug and Play With SwiftUI 9 | - Caching with NSCache 10 | - VisionOS Support 11 | 12 | ## Usage 13 | 14 | ```swift 15 | static var previews: some View { 16 | SVGImage(url:URL(string:"https://crests.football-data.org/113.svg")!, size: CGSize(width: 100,height: 100)) 17 | } 18 | ``` 19 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.6 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | import PackageDescription 5 | 6 | let package = Package( 7 | name: "SVGImageSwiftUI", 8 | platforms: [ 9 | .iOS(.v13), 10 | .tvOS(.v13) 11 | ], 12 | products: [ 13 | .library( 14 | name: "SVGImageSwiftUI", 15 | targets: ["SVGImageSwiftUI"]), 16 | ], 17 | dependencies: [ 18 | .package(url: "https://github.com/alfianlosari/SVGKit", branch: "3.x") 19 | ], 20 | targets: [ 21 | .target( 22 | name: "SVGImageSwiftUI", 23 | dependencies: [ 24 | .product(name: "SVGKit", package: "SVGKit"), 25 | .product(name: "SVGKitSwift", package: "SVGKit") 26 | ] 27 | ) 28 | ] 29 | ) 30 | -------------------------------------------------------------------------------- /Package.resolved: -------------------------------------------------------------------------------- 1 | { 2 | "pins" : [ 3 | { 4 | "identity" : "cocoalumberjack", 5 | "kind" : "remoteSourceControl", 6 | "location" : "https://github.com/CocoaLumberjack/CocoaLumberjack.git", 7 | "state" : { 8 | "revision" : "0188d31089b5881a269e01777be74c7316924346", 9 | "version" : "3.8.0" 10 | } 11 | }, 12 | { 13 | "identity" : "svgkit", 14 | "kind" : "remoteSourceControl", 15 | "location" : "https://github.com/alfianlosari/SVGKit", 16 | "state" : { 17 | "branch" : "3.x", 18 | "revision" : "9306d7c06eaa59af1fd0efe01edec45ef997025a" 19 | } 20 | }, 21 | { 22 | "identity" : "swift-log", 23 | "kind" : "remoteSourceControl", 24 | "location" : "https://github.com/apple/swift-log.git", 25 | "state" : { 26 | "revision" : "32e8d724467f8fe623624570367e3d50c5638e46", 27 | "version" : "1.5.2" 28 | } 29 | } 30 | ], 31 | "version" : 2 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Alfian Losari 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Sources/SVGImageSwiftUI/SVGImageSwiftUI.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | import SVGKit 3 | 4 | let cache = NSCache() 5 | 6 | public struct SVGImage: UIViewRepresentable { 7 | public let url: URL 8 | public let size: CGSize 9 | 10 | public init(url: URL, size: CGSize) { 11 | self.url = url 12 | self.size = size 13 | } 14 | 15 | public func updateUIView(_ imageView: SVGKFastImageView, context: Context) { 16 | if let image = cache.object(forKey: url.absoluteString as NSString) { 17 | imageView.image = image 18 | imageView.image.size = size 19 | } else { 20 | let url = self.url 21 | DispatchQueue.global(qos: .userInitiated).async { 22 | if let svgImage = SVGKImage(contentsOf: url) { 23 | cache.setObject(svgImage, forKey: url.absoluteString as NSString) 24 | DispatchQueue.main.async { 25 | guard url == self.url else { return } 26 | imageView.image = svgImage 27 | imageView.backgroundColor = .clear 28 | imageView.image.size = size 29 | } 30 | } 31 | } 32 | } 33 | } 34 | 35 | public func makeUIView(context: Context) -> SVGKFastImageView { 36 | let imgView: SVGKFastImageView = SVGKFastImageView(svgkImage: SVGKImage()) 37 | imgView.backgroundColor = .gray.withAlphaComponent(0.5) 38 | imgView.contentMode = .scaleAspectFit 39 | return imgView 40 | 41 | } 42 | } 43 | 44 | 45 | --------------------------------------------------------------------------------