aboutsummaryrefslogtreecommitdiff
path: root/Demo/SwiftyCropDemo/ContentView.swift
diff options
context:
space:
mode:
authorBen <31181527+benedom@users.noreply.github.com>2024-01-26 15:33:41 +0100
committerGitHub <noreply@github.com>2024-01-26 15:33:41 +0100
commit74bf9219d718ee1b47a2cde7d235ac3f73992df9 (patch)
treebee70f28097261f833a52f47d15ce9bf82f918bf /Demo/SwiftyCropDemo/ContentView.swift
parentf8cfea0bf31c889d431678fea0f86e39e2819761 (diff)
parent9d895fbac4c7c653a883969ac3520f1847d299e8 (diff)
Merge pull request #3 from leoz/master
Add demo app
Diffstat (limited to 'Demo/SwiftyCropDemo/ContentView.swift')
-rw-r--r--Demo/SwiftyCropDemo/ContentView.swift102
1 files changed, 102 insertions, 0 deletions
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()
+}