diff options
author | Ben <31181527+benedom@users.noreply.github.com> | 2024-01-26 15:33:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-26 15:33:41 +0100 |
commit | 74bf9219d718ee1b47a2cde7d235ac3f73992df9 (patch) | |
tree | bee70f28097261f833a52f47d15ce9bf82f918bf /Demo/SwiftyCropDemo | |
parent | f8cfea0bf31c889d431678fea0f86e39e2819761 (diff) | |
parent | 9d895fbac4c7c653a883969ac3520f1847d299e8 (diff) |
Merge pull request #3 from leoz/master
Add demo app
Diffstat (limited to 'Demo/SwiftyCropDemo')
-rw-r--r-- | Demo/SwiftyCropDemo/Assets.xcassets/AccentColor.colorset/Contents.json | 11 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/Assets.xcassets/AppIcon.appiconset/Contents.json | 13 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/Assets.xcassets/Contents.json | 6 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/ContentView.swift | 102 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/LongText.swift | 21 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/Preview Content/Preview Assets.xcassets/Contents.json | 6 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/ShapeButton.swift | 32 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/SwiftyCropDemo.xcconfig | 15 | ||||
-rw-r--r-- | Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift | 17 |
9 files changed, 223 insertions, 0 deletions
diff --git a/Demo/SwiftyCropDemo/Assets.xcassets/AccentColor.colorset/Contents.json b/Demo/SwiftyCropDemo/Assets.xcassets/AccentColor.colorset/Contents.json new file mode 100644 index 0000000..eb87897 --- /dev/null +++ b/Demo/SwiftyCropDemo/Assets.xcassets/AccentColor.colorset/Contents.json @@ -0,0 +1,11 @@ +{ + "colors" : [ + { + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Demo/SwiftyCropDemo/Assets.xcassets/AppIcon.appiconset/Contents.json b/Demo/SwiftyCropDemo/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..13613e3 --- /dev/null +++ b/Demo/SwiftyCropDemo/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,13 @@ +{ + "images" : [ + { + "idiom" : "universal", + "platform" : "ios", + "size" : "1024x1024" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Demo/SwiftyCropDemo/Assets.xcassets/Contents.json b/Demo/SwiftyCropDemo/Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Demo/SwiftyCropDemo/Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Demo/SwiftyCropDemo/ContentView.swift b/Demo/SwiftyCropDemo/ContentView.swift new file mode 100644 index 0000000..c999dd3 --- /dev/null +++ b/Demo/SwiftyCropDemo/ContentView.swift @@ -0,0 +1,102 @@ +// +// ContentView.swift +// SwiftyCropDemo +// +// Created by Leonid Zolotarev on 1/23/24. +// + +import SwiftUI +import SwiftyCrop + +struct ContentView: View { + @State private var showImageCropper: Bool = false + @State private var selectedImage: UIImage? + @State private var selectedShape: MaskShape = .square + @State private var cropImageCircular: Bool = false + + var body: some View { + VStack { + Spacer() + Group { + if let selectedImage = selectedImage { + Image(uiImage: selectedImage) + .resizable() + .aspectRatio(contentMode: .fit) + .cornerRadius(8) + } else { + ProgressView() + } + } + .scaledToFit() + .padding() + Spacer() + GroupBox { + HStack { + Button { + loadImage() + } label: { + LongText(title: "Load image") + } + Button { + showImageCropper.toggle() + } label: { + LongText(title: "Crop image") + } + } + HStack { + ShapeButton( + title: "Use square crop", + shape: .square, + selection: $selectedShape + ) + ShapeButton( + title: "Use circle crop", + shape: .circle, + selection: $selectedShape + ) + } + Toggle("Crop image to circle", isOn: $cropImageCircular) + } + .buttonStyle(.bordered) + .padding() + } + .onAppear { + loadImage() + } + .fullScreenCover(isPresented: $showImageCropper) { + if let selectedImage = selectedImage { + SwiftyCropView( + imageToCrop: selectedImage, + maskShape: selectedShape, + configuration: SwiftyCropConfiguration( + cropImageCircular: cropImageCircular + ) + ) { croppedImage in + // Do something with the returned, cropped image + self.selectedImage = croppedImage + } + } + } + } + + private func loadImage() { + Task { + selectedImage = await downloadExampleImage() + } + } + + // Example function for downloading an image + private func downloadExampleImage() async -> UIImage? { + let urlString = "https://picsum.photos/1000/1200" + guard let url = URL(string: urlString), + let (data, _) = try? await URLSession.shared.data(from: url), + let image = UIImage(data: data) + else { return nil } + + return image + } +} + +#Preview { + ContentView() +} diff --git a/Demo/SwiftyCropDemo/LongText.swift b/Demo/SwiftyCropDemo/LongText.swift new file mode 100644 index 0000000..77e211a --- /dev/null +++ b/Demo/SwiftyCropDemo/LongText.swift @@ -0,0 +1,21 @@ +// +// LongText.swift +// SwiftyCropDemo +// +// Created by Leonid Zolotarev on 1/24/24. +// + +import SwiftUI + +struct LongText: View { + let title: String + + var body: some View { + Text(title) + .frame(maxWidth: .infinity) + } +} + +#Preview { + LongText(title: "title") +} diff --git a/Demo/SwiftyCropDemo/Preview Content/Preview Assets.xcassets/Contents.json b/Demo/SwiftyCropDemo/Preview Content/Preview Assets.xcassets/Contents.json new file mode 100644 index 0000000..73c0059 --- /dev/null +++ b/Demo/SwiftyCropDemo/Preview Content/Preview Assets.xcassets/Contents.json @@ -0,0 +1,6 @@ +{ + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/Demo/SwiftyCropDemo/ShapeButton.swift b/Demo/SwiftyCropDemo/ShapeButton.swift new file mode 100644 index 0000000..c7aae9f --- /dev/null +++ b/Demo/SwiftyCropDemo/ShapeButton.swift @@ -0,0 +1,32 @@ +// +// ShapeButton.swift +// SwiftyCropDemo +// +// Created by Leonid Zolotarev on 1/24/24. +// + +import SwiftUI +import SwiftyCrop + +struct ShapeButton: View { + let title: String + let shape: MaskShape + @Binding var selection: MaskShape + + var body: some View { + Button { + selection = shape + } label: { + LongText(title: title) + } + .foregroundColor(selection == shape ? .accentColor : .secondary) + } +} + +#Preview { + ShapeButton( + title: "title", + shape: .circle, + selection: .constant(.circle) + ) +} diff --git a/Demo/SwiftyCropDemo/SwiftyCropDemo.xcconfig b/Demo/SwiftyCropDemo/SwiftyCropDemo.xcconfig new file mode 100644 index 0000000..42270da --- /dev/null +++ b/Demo/SwiftyCropDemo/SwiftyCropDemo.xcconfig @@ -0,0 +1,15 @@ +// +// SwiftyCropDemo.xcconfig +// SwiftyCropDemo +// +// Created by Leonid Zolotarev on 1/26/24. +// + +// Configuration settings file format documentation can be found at: +// https://help.apple.com/xcode/#/dev745c5c974 + +// +DEVELOPMENT_TEAM = + +// +PRODUCT_BUNDLE_IDENTIFIER = com.leoz.SwiftyCropDemo diff --git a/Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift b/Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift new file mode 100644 index 0000000..fb7e2bb --- /dev/null +++ b/Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift @@ -0,0 +1,17 @@ +// +// SwiftyCropDemoApp.swift +// SwiftyCropDemo +// +// Created by Leonid Zolotarev on 1/23/24. +// + +import SwiftUI + +@main +struct SwiftyCropDemoApp: App { + var body: some Scene { + WindowGroup { + ContentView() + } + } +} |