add visionos support

This commit is contained in:
infinitepower18 2024-03-19 01:04:12 +00:00 committed by Shibo Lyu
parent c296f3e6e5
commit 1d66f10bff
4 changed files with 10 additions and 9 deletions

View file

@ -1,4 +1,4 @@
// swift-tools-version: 5.8 // swift-tools-version: 5.10
// The swift-tools-version declares the minimum version of Swift required to build this package. // The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
@ -7,7 +7,8 @@ let package = Package(
name: "CropImage", name: "CropImage",
platforms: [ platforms: [
.iOS(.v14), .iOS(.v14),
.macOS(.v13) .macOS(.v13),
.visionOS(.v1)
], ],
products: [ products: [
// Products define the executables and libraries a package produces, and make them visible to other packages. // Products define the executables and libraries a package produces, and make them visible to other packages.

View file

@ -6,7 +6,7 @@
// //
import SwiftUI import SwiftUI
#if os(iOS) #if !os(macOS)
import UIKit import UIKit
#endif #endif
@ -144,13 +144,13 @@ public struct CropImageView<Controls: View, CutHole: View>: View {
if #available(iOS 16.0, macOS 13.0, *) { if #available(iOS 16.0, macOS 13.0, *) {
let renderer = ImageRenderer(content: snapshotView) let renderer = ImageRenderer(content: snapshotView)
renderer.scale = targetScale renderer.scale = targetScale
#if os(iOS) #if !os(macOS)
if let image = renderer.uiImage { if let image = renderer.uiImage {
return image return image
} else { } else {
throw CropError.imageRendererReturnedNil throw CropError.imageRendererReturnedNil
} }
#elseif os(macOS) #else
if let image = renderer.nsImage { if let image = renderer.nsImage {
return image return image
} else { } else {
@ -160,7 +160,7 @@ public struct CropImageView<Controls: View, CutHole: View>: View {
} else { } else {
#if os(macOS) #if os(macOS)
fatalError("Cropping is not supported on macOS versions before Ventura 13.0.") fatalError("Cropping is not supported on macOS versions before Ventura 13.0.")
#elseif os(iOS) #else
let window = UIWindow(frame: CGRect(origin: .zero, size: targetSize)) let window = UIWindow(frame: CGRect(origin: .zero, size: targetSize))
let hosting = UIHostingController(rootView: snapshotView) let hosting = UIHostingController(rootView: snapshotView)
hosting.view.frame = window.frame hosting.view.frame = window.frame
@ -250,7 +250,7 @@ struct CropImageView_Previews: PreviewProvider {
case let .success(croppedImage): case let .success(croppedImage):
#if os(macOS) #if os(macOS)
Image(nsImage: croppedImage) Image(nsImage: croppedImage)
#elseif os(iOS) #else
Image(uiImage: croppedImage) Image(uiImage: croppedImage)
#endif #endif
case let .failure(error): case let .failure(error):

View file

@ -13,7 +13,7 @@ import AppKit
/// ///
/// On macOS, it's `NSImage` and on iOS it's `UIImage`. /// On macOS, it's `NSImage` and on iOS it's `UIImage`.
public typealias PlatformImage = NSImage public typealias PlatformImage = NSImage
#elseif os(iOS) #else
import UIKit import UIKit
/// The image object type, aliased to each platform. /// The image object type, aliased to each platform.
/// ///

View file

@ -84,7 +84,7 @@ struct UnderlyingImageView: View {
var imageView: Image { var imageView: Image {
#if os(macOS) #if os(macOS)
Image(nsImage: image) Image(nsImage: image)
#elseif os(iOS) #else
Image(uiImage: image) Image(uiImage: image)
#endif #endif
} }