diff options
author | leoz <leoz@yahoo.com> | 2024-01-24 18:36:52 -0500 |
---|---|---|
committer | leoz <leoz@yahoo.com> | 2024-01-24 18:36:52 -0500 |
commit | 0c38214d3ce5b24352e0fcea53765118d66a76d9 (patch) | |
tree | 3bc377005892bf93129a34e0366efff33ede1645 /Demo/SwiftyCropDemo/ContentView.swift | |
parent | f8cfea0bf31c889d431678fea0f86e39e2819761 (diff) |
Add demo app
Diffstat (limited to 'Demo/SwiftyCropDemo/ContentView.swift')
-rw-r--r-- | Demo/SwiftyCropDemo/ContentView.swift | 102 |
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() +} |