diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 16583b6..c03dc88 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -60,4 +60,4 @@ jobs: - name: Build run: | cd Examples - xcodebuild build -scheme 'Cassini' -destination 'name=iPhone 14' -sdk iphoneos + xcodebuild build -scheme 'Cassini' -destination 'platform=iOS Simulator,name=iPhone 14' -sdk iphonesimulator diff --git a/Sources/GeoDrawer/GeoDrawer+UIKit.swift b/Sources/GeoDrawer/GeoDrawer+Image.swift similarity index 55% rename from Sources/GeoDrawer/GeoDrawer+UIKit.swift rename to Sources/GeoDrawer/GeoDrawer+Image.swift index c33b082..fefb802 100644 --- a/Sources/GeoDrawer/GeoDrawer+UIKit.swift +++ b/Sources/GeoDrawer/GeoDrawer+Image.swift @@ -1,5 +1,5 @@ // -// GeoDrawer+UIKit.swift +// GeoDrawer+Image.swift // // // Created by Adrian Schönig on 2/12/2022. @@ -10,8 +10,6 @@ #if canImport(UIKit) import UIKit -// MARK: - Image drawing - extension GeoDrawer { /// Generates an image drawing the provided contents according to the configured @@ -44,4 +42,37 @@ extension GeoDrawer { } +#elseif canImport(AppKit) +import AppKit + +extension GeoDrawer { + + /// Generates an image drawing the provided contents according to the configured + /// projection, zoom and edge insets. When providing the different colours, the background of + /// the map projection itself will be drawn, too. + /// + /// - Parameters: + /// - contents: GeoJSON contents to draw + /// - mapBackground: Background of the map itself + /// - mapOutline: Stroke colour around the map + /// - mapBackdrop: Background to draw outside the map / projection bounds + /// - Returns: New image + public func drawImage(_ contents: [Content], mapBackground: NSColor? = nil, mapOutline: NSColor? = nil, mapBackdrop: NSColor? = nil) -> NSImage { + + let cgSize = CGSize(width: size.width, height: size.height) + return NSImage(size: cgSize, flipped: false) { _ in + guard let context = NSGraphicsContext.current?.cgContext else { return false } + self.draw( + contents, + mapBackground: mapBackground?.cgColor, + mapOutline: mapOutline?.cgColor, + mapBackdrop: mapBackdrop?.cgColor, + in: context + ) + return true + } + } + +} + #endif