Skip to content

Commit

Permalink
Add image extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Sep 16, 2024
1 parent ec11903 commit 3bbe645
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
31 changes: 31 additions & 0 deletions Sources/OversizeCore/Extensions/Image/Image+Data.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
41 changes: 41 additions & 0 deletions Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import MapKit

extension CLLocationCoordinate2D: Identifiable {
extension CLLocationCoordinate2D: @retroactive Identifiable {
public var id: String {
"\(latitude)-\(longitude)"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/OversizeCore/Extensions/Swift/Date+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public extension Date {
}
}

extension Date: RawRepresentable {
extension Date: @retroactive RawRepresentable {
private static let formatter: ISO8601DateFormatter = .init()

public var rawValue: String {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }
}

0 comments on commit 3bbe645

Please sign in to comment.