-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec11903
commit 3bbe645
Showing
6 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
Sources/OversizeCore/Extensions/Image/NSImage+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Sources/OversizeCore/Extensions/SwiftData/SortOrder+Extension.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |