Official iOS/iPadOS/visionOS/tvOS support

This commit is contained in:
Alex Zenla
2024-09-03 05:38:32 -04:00
parent 788cd28994
commit 224bb6616c
10 changed files with 1228 additions and 3 deletions

View File

@ -1,4 +1,9 @@
#if os(macOS)
import AppKit
#else
import UIKit
#endif
struct NSImageLoader {
private static let flipVertically = true
@ -6,11 +11,19 @@ struct NSImageLoader {
static func open(url: URL) throws -> Image2D {
try autoreleasepool {
// Open as a CoreGraphics image
#if os(macOS)
guard let nsImage = NSImage(contentsOf: url),
let cgImage = nsImage.cgImage(forProposedRect: nil, context: nil, hints: nil)
else {
throw ImageLoaderError.openFailed("Failed to open image \"\(url.absoluteString)\"")
}
#else
guard let nsImage = UIImage(contentsOfFile: url.path(percentEncoded: false)),
let cgImage = nsImage.cgImage
else {
throw ImageLoaderError.openFailed("Failed to open image \"\(url.absoluteString)\"")
}
#endif
// Convert 8-bit ARGB (sRGB) w/ pre-multiplied alpha
let alphaInfo = cgImage.alphaInfo == .none