diff options
Diffstat (limited to 'Demo')
-rw-r--r-- | Demo/SwiftyCropDemo.xcodeproj/project.pbxproj | 8 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/ContentView.swift | 53 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/UIElements/DecimalTextField.swift | 2 |
3 files changed, 58 insertions, 5 deletions
diff --git a/Demo/SwiftyCropDemo.xcodeproj/project.pbxproj b/Demo/SwiftyCropDemo.xcodeproj/project.pbxproj index 5609787..dcf869d 100644 --- a/Demo/SwiftyCropDemo.xcodeproj/project.pbxproj +++ b/Demo/SwiftyCropDemo.xcodeproj/project.pbxproj @@ -309,9 +309,13 @@ "$(inherited)", "@executable_path/Frameworks", ); + MACOSX_DEPLOYMENT_TARGET = 12.0; MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = "--PRODUCT-BUNDLE-IDENTIFIER-"; PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx"; + SUPPORTS_MACCATALYST = NO; + SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; @@ -337,9 +341,13 @@ "$(inherited)", "@executable_path/Frameworks", ); + MACOSX_DEPLOYMENT_TARGET = 12.0; MARKETING_VERSION = 1.0; PRODUCT_BUNDLE_IDENTIFIER = "--PRODUCT-BUNDLE-IDENTIFIER-"; PRODUCT_NAME = "$(TARGET_NAME)"; + SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx"; + SUPPORTS_MACCATALYST = NO; + SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_VERSION = 5.0; TARGETED_DEVICE_FAMILY = "1,2"; diff --git a/Demo/SwiftyCropDemo/ContentView.swift b/Demo/SwiftyCropDemo/ContentView.swift index 08b1060..d4a7d5d 100644 --- a/Demo/SwiftyCropDemo/ContentView.swift +++ b/Demo/SwiftyCropDemo/ContentView.swift @@ -1,9 +1,15 @@ import SwiftUI import SwiftyCrop +#if canImport(UIKit) +typealias PlatformImage = UIImage +#elseif canImport(AppKit) +typealias PlatformImage = NSImage +#endif + struct ContentView: View { @State private var showImageCropper: Bool = false - @State private var selectedImage: UIImage? + @State private var selectedImage: PlatformImage? @State private var selectedShape: MaskShape = .square @State private var cropImageCircular: Bool @State private var rotateImage: Bool @@ -27,8 +33,7 @@ struct ContentView: View { Group { if let selectedImage = selectedImage { - Image(uiImage: selectedImage) - .resizable() + PlatformImageView(image: selectedImage) .aspectRatio(contentMode: .fit) .cornerRadius(8) } else { @@ -84,7 +89,15 @@ struct ContentView: View { .frame(maxWidth: .infinity, alignment: .leading) Button { +#if canImport(UIKit) maskRadius = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) / 2 +#elseif canImport(AppKit) + if let screen = NSScreen.main { + maskRadius = min(screen.frame.width, screen.frame.height) / 2 + } else { + maskRadius = 200 // Default value if no screen is available + } +#endif } label: { Image(systemName: "arrow.up.left.and.arrow.down.right") .font(.footnote) @@ -119,7 +132,19 @@ struct ContentView: View { .onAppear { loadImage() } + #if os(macOS) + .sheet(isPresented: $showImageCropper) { + imageCropperView + } + #else .fullScreenCover(isPresented: $showImageCropper) { + imageCropperView + } + #endif + } + + private var imageCropperView: some View { + Group { if let selectedImage = selectedImage { SwiftyCropView( imageToCrop: selectedImage, @@ -137,6 +162,9 @@ struct ContentView: View { } } } + #if canImport(AppKit) + .frame(width: 600, height: 400) // Adjust size as needed for macOS + #endif } private func loadImage() { @@ -146,19 +174,34 @@ struct ContentView: View { } // Example function for downloading an image - private func downloadExampleImage() async -> UIImage? { + private func downloadExampleImage() async -> PlatformImage? { let portraitUrlString = "https://picsum.photos/1000/1200" let landscapeUrlString = "https://picsum.photos/2000/1000" let urlString = Int.random(in: 0...1) == 0 ? portraitUrlString : landscapeUrlString guard let url = URL(string: urlString), let (data, _) = try? await URLSession.shared.data(from: url), - let image = UIImage(data: data) + let image = PlatformImage(data: data) else { return nil } return image } } +struct PlatformImageView: View { + let image: PlatformImage + + var body: some View { + #if canImport(UIKit) + Image(uiImage: image) + .resizable() + #elseif canImport(AppKit) + Image(nsImage: image) + .resizable() + #endif + } +} + + #Preview { ContentView() } diff --git a/Demo/SwiftyCropDemo/UIElements/DecimalTextField.swift b/Demo/SwiftyCropDemo/UIElements/DecimalTextField.swift index f616413..9758f0f 100644 --- a/Demo/SwiftyCropDemo/UIElements/DecimalTextField.swift +++ b/Demo/SwiftyCropDemo/UIElements/DecimalTextField.swift @@ -15,6 +15,8 @@ struct DecimalTextField: View { TextField("maxMagnification", value: $value, formatter: decimalFormatter) .textFieldStyle(RoundedBorderTextFieldStyle()) .multilineTextAlignment(.trailing) + #if canImport(UIKit) .keyboardType(.decimalPad) + #endif } } |