diff --git a/Examples/Basic.xcodeproj/project.pbxproj b/Examples/Basic.xcodeproj/project.pbxproj index 813eb65..9edf96f 100644 --- a/Examples/Basic.xcodeproj/project.pbxproj +++ b/Examples/Basic.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ D961846D220FDD1200C59D9B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D961846C220FDD1200C59D9B /* Assets.xcassets */; }; D9618470220FDD1200C59D9B /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D961846E220FDD1200C59D9B /* LaunchScreen.storyboard */; }; D9742E1E27D4877300E02FFD /* SwiftDraw in Frameworks */ = {isa = PBXBuildFile; productRef = D9742E1D27D4877300E02FFD /* SwiftDraw */; }; + D9AC57BF2D65E86A005ACBFF /* GalleryView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */; }; D9EE86AF2A4EC94E00C7CAE1 /* Samples.bundle in Resources */ = {isa = PBXBuildFile; fileRef = D94D5BE22A4EC906001DCD83 /* Samples.bundle */; }; /* End PBXBuildFile section */ @@ -37,6 +38,7 @@ D961846F220FDD1200C59D9B /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; D9618471220FDD1200C59D9B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; D9742E1C27D4875100E02FFD /* SwiftDraw */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SwiftDraw; path = ..; sourceTree = ""; }; + D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GalleryView.swift; sourceTree = ""; }; D9ACD7A0220FDE04009717CF /* SwiftDraw.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftDraw.framework; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ @@ -75,6 +77,7 @@ D9618464220FDD1100C59D9B /* Sources */ = { isa = PBXGroup; children = ( + D9AC57BE2D65E86A005ACBFF /* GalleryView.swift */, D9618465220FDD1100C59D9B /* AppDelegate.swift */, D9618467220FDD1100C59D9B /* ViewController.swift */, D961846C220FDD1200C59D9B /* Assets.xcassets */, @@ -169,6 +172,7 @@ files = ( D9618468220FDD1100C59D9B /* ViewController.swift in Sources */, D9618466220FDD1100C59D9B /* AppDelegate.swift in Sources */, + D9AC57BF2D65E86A005ACBFF /* GalleryView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Examples/Sources/AppDelegate.swift b/Examples/Sources/AppDelegate.swift index c5fde6d..5c3aede 100644 --- a/Examples/Sources/AppDelegate.swift +++ b/Examples/Sources/AppDelegate.swift @@ -30,6 +30,7 @@ // import UIKit +import SwiftUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { @@ -40,7 +41,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let window = UIWindow() - window.rootViewController = UINavigationController(rootViewController: ViewController()) + window.rootViewController = UINavigationController(rootViewController: UIHostingController(rootView: GalleryView())) window.makeKeyAndVisible() self.window = window diff --git a/Examples/Sources/GalleryView.swift b/Examples/Sources/GalleryView.swift new file mode 100644 index 0000000..bb09ceb --- /dev/null +++ b/Examples/Sources/GalleryView.swift @@ -0,0 +1,69 @@ +// +// GalleryView.swift +// SwiftDraw +// +// Created by Simon Whitty on 19/2/25. +// Copyright 2019 Simon Whitty +// +// Distributed under the permissive zlib license +// Get the latest version from here: +// +// https://github.com/swhitty/SwiftDraw +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// + +import SwiftDraw +import SwiftUI + +struct GalleryView: View { + + var imageNames: [String] = [ + "avocado.svg", + "angry.svg", + "dish.svg", + "mouth-open.svg", + "sleepy.svg", + "smile.svg", + "snake.svg", + "spider.svg", + "star-struck.svg", + "worried.svg", + "yawning.svg", + "thats-no-moon.svg", + "alert.svg" + ] + + var body: some View { + if #available(iOS 15.0, *) { + ScrollView { + LazyVStack(spacing: 20) { + ForEach(imageNames, id: \.self) { name in + SVGView(name, bundle: .samples) + .aspectRatio(contentMode: .fit) + .padding([.leading, .trailing], 10) + // .frame(maxWidth: 320) + } + } + .background(Color.white) + } + + } + } +} diff --git a/SwiftDraw/Image+CoreGraphics.swift b/SwiftDraw/SVG+CoreGraphics.swift similarity index 100% rename from SwiftDraw/Image+CoreGraphics.swift rename to SwiftDraw/SVG+CoreGraphics.swift diff --git a/SwiftDraw/Image.swift b/SwiftDraw/SVG.swift similarity index 98% rename from SwiftDraw/Image.swift rename to SwiftDraw/SVG.swift index 0c1d519..3528d6c 100644 --- a/SwiftDraw/Image.swift +++ b/SwiftDraw/SVG.swift @@ -73,8 +73,8 @@ public final class SVG: NSObject { } } -@available(*, deprecated, renamed: "SVG") -public typealias Image = SVG +@available(*, unavailable, renamed: "SVG") +public enum Image { } #else diff --git a/SwiftDraw/SVGView.swift b/SwiftDraw/SVGView.swift new file mode 100644 index 0000000..976992a --- /dev/null +++ b/SwiftDraw/SVGView.swift @@ -0,0 +1,71 @@ +// +// SVGView.swift +// SwiftDraw +// +// Created by Simon Whitty on 19/2/25. +// Copyright 2019 Simon Whitty +// +// Distributed under the permissive zlib license +// Get the latest version from here: +// +// https://github.com/swhitty/SwiftDraw +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source distribution. +// + +#if canImport(SwiftUI) +import SwiftUI + +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) +public struct SVGView: View { + + public init(_ name: String, bundle: Bundle = .main) { + self.svg = SVG(named: name, in: bundle) + } + + public init(svg: SVG) { + self.svg = svg + } + + private let svg: SVG? + + public var body: some View { + if let svg { + Canvas( + opaque: false, + colorMode: .linear, + rendersAsynchronously: false + ) { ctx, size in + ctx.draw(svg, in: CGRect(origin: .zero, size: size)) + } + .frame(idealWidth: svg.size.width, idealHeight: svg.size.height) + } + } +} + +@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *) +public extension GraphicsContext { + + func draw(_ image: SVG, in rect: CGRect? = nil) { + withCGContext { + $0.draw(image, in: rect) + } + } +} +#endif