aboutsummaryrefslogtreecommitdiff
path: root/Demo/SwiftyCropDemo
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/SwiftyCropDemo')
-rw-r--r--Demo/SwiftyCropDemo/Assets.xcassets/AccentColor.colorset/Contents.json11
-rw-r--r--Demo/SwiftyCropDemo/Assets.xcassets/AppIcon.appiconset/Contents.json13
-rw-r--r--Demo/SwiftyCropDemo/Assets.xcassets/Contents.json6
-rw-r--r--Demo/SwiftyCropDemo/ContentView.swift102
-rw-r--r--Demo/SwiftyCropDemo/LongText.swift21
-rw-r--r--Demo/SwiftyCropDemo/Preview Content/Preview Assets.xcassets/Contents.json6
-rw-r--r--Demo/SwiftyCropDemo/ShapeButton.swift32
-rw-r--r--Demo/SwiftyCropDemo/SwiftyCropDemoApp.swift17
8 files changed, 208 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/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()
+ }
+ }
+}