Skip to content

Instantly share code, notes, and snippets.

@heestand-xyz
Created July 5, 2020 19:40
Show Gist options
  • Save heestand-xyz/5e8bc95275e5d1827a592c2b3bf1c921 to your computer and use it in GitHub Desktop.
Save heestand-xyz/5e8bc95275e5d1827a592c2b3bf1c921 to your computer and use it in GitHub Desktop.
Some Image
#if os(macOS)
import AppKit
#else
import UIKit
#endif
import SwiftUI
#if os(macOS)
typealias SomeImage = NSImage
#else
typealias SomeImage = UIImage
#endif
#if os(macOS)
extension Data {
var bitmap: NSBitmapImageRep? { NSBitmapImageRep(data: self) }
}
extension NSBitmapImageRep {
var png: Data? { representation(using: .png, properties: [:]) }
}
extension SomeImage {
func pngData() -> Data? {
tiffRepresentation?.bitmap?.png
}
}
#endif
extension Image {
init(image: SomeImage) {
#if os(macOS)
self.init(nsImage: image)
#else
self.init(uiImage: image)
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment