diff --git a/Sources/OversizeCore/Extensions/Image/Image+Data.swift b/Sources/OversizeCore/Extensions/Image/Image+Data.swift new file mode 100644 index 0000000..040ea54 --- /dev/null +++ b/Sources/OversizeCore/Extensions/Image/Image+Data.swift @@ -0,0 +1,31 @@ +// +// Copyright © 2024 Alexander Romanov +// Image+Data.swift, created on 27.06.2024 +// + +#if canImport(UIKit) + import UIKit +#elseif canImport(AppKit) + import AppKit +#endif +import SwiftUI + +public extension Image { + init?(data: Data) { + #if canImport(UIKit) + if let uiImage = UIImage(data: data) { + self.init(uiImage: uiImage) + } else { + return nil + } + #elseif canImport(AppKit) + if let nsImage = NSImage(data: data) { + self.init(nsImage: nsImage) + } else { + return nil + } + #else + return nil + #endif + } +} diff --git a/Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift b/Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift new file mode 100644 index 0000000..b45ade6 --- /dev/null +++ b/Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift @@ -0,0 +1,41 @@ +// +// Copyright © 2024 Alexander Romanov +// NSImage+Extension.swift, created on 27.06.2024 +// + +#if canImport(AppKit) + import AppKit +#endif + +#if canImport(AppKit) + public extension NSImage { + func pngData() -> Data? { + guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { + return nil + } + + let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage) + return bitmapRepresentation.representation(using: .png, properties: [:]) + } + } + + public extension NSImage { + func jpegData(compressionQuality _: CGFloat) -> Data? { + guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { + return nil + } + + let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage) + return bitmapRepresentation.representation(using: .jpeg, properties: [:]) + } + + func jpegData() -> Data? { + guard let cgImage = cgImage(forProposedRect: nil, context: nil, hints: nil) else { + return nil + } + + let bitmapRepresentation = NSBitmapImageRep(cgImage: cgImage) + return bitmapRepresentation.representation(using: .jpeg, properties: [:]) + } + } +#endif diff --git a/Sources/OversizeCore/Extensions/Map/CLLocationCoordinate2D+Extension.swift b/Sources/OversizeCore/Extensions/Map/CLLocationCoordinate2D+Extension.swift index 86bc185..89bba65 100644 --- a/Sources/OversizeCore/Extensions/Map/CLLocationCoordinate2D+Extension.swift +++ b/Sources/OversizeCore/Extensions/Map/CLLocationCoordinate2D+Extension.swift @@ -5,7 +5,7 @@ import MapKit -extension CLLocationCoordinate2D: Identifiable { +extension CLLocationCoordinate2D: @retroactive Identifiable { public var id: String { "\(latitude)-\(longitude)" } diff --git a/Sources/OversizeCore/Extensions/Swift/Array+Extension.swift b/Sources/OversizeCore/Extensions/Swift/Array+Extension.swift index f5f8c48..8c9fc23 100644 --- a/Sources/OversizeCore/Extensions/Swift/Array+Extension.swift +++ b/Sources/OversizeCore/Extensions/Swift/Array+Extension.swift @@ -123,7 +123,7 @@ public extension Array where Element: Equatable { } } -extension Array: RawRepresentable where Element: Codable { +extension Array: @retroactive RawRepresentable where Element: Codable { public init?(rawValue: String) { guard let data = rawValue.data(using: .utf8), let result = try? JSONDecoder().decode([Element].self, from: data) diff --git a/Sources/OversizeCore/Extensions/Swift/Date+Extension.swift b/Sources/OversizeCore/Extensions/Swift/Date+Extension.swift index f92075c..0f7920d 100644 --- a/Sources/OversizeCore/Extensions/Swift/Date+Extension.swift +++ b/Sources/OversizeCore/Extensions/Swift/Date+Extension.swift @@ -131,7 +131,7 @@ public extension Date { } } -extension Date: RawRepresentable { +extension Date: @retroactive RawRepresentable { private static let formatter: ISO8601DateFormatter = .init() public var rawValue: String { diff --git a/Sources/OversizeCore/Extensions/SwiftData/SortOrder+Extension.swift b/Sources/OversizeCore/Extensions/SwiftData/SortOrder+Extension.swift new file mode 100644 index 0000000..d0f49dc --- /dev/null +++ b/Sources/OversizeCore/Extensions/SwiftData/SortOrder+Extension.swift @@ -0,0 +1,21 @@ +// +// Copyright © 2024 Alexander Romanov +// SortOrder+Extension.swift, created on 28.06.2024 +// + +import Foundation + +extension SortOrder: @retroactive CaseIterable, @retroactive Identifiable { + public static var allCases: [SortOrder] = [.forward, .reverse] + + public var title: String { + switch self { + case .forward: + "Ascending" + case .reverse: + "Descending" + } + } + + public var id: String { title } +}