aboutsummaryrefslogtreecommitdiff
path: root/Demo/SwiftyCropDemo/ContentView.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Demo/SwiftyCropDemo/ContentView.swift')
-rw-r--r--Demo/SwiftyCropDemo/ContentView.swift115
1 files changed, 80 insertions, 35 deletions
diff --git a/Demo/SwiftyCropDemo/ContentView.swift b/Demo/SwiftyCropDemo/ContentView.swift
index cfb1f01..f4e90ee 100644
--- a/Demo/SwiftyCropDemo/ContentView.swift
+++ b/Demo/SwiftyCropDemo/ContentView.swift
@@ -1,10 +1,3 @@
-//
-// ContentView.swift
-// SwiftyCropDemo
-//
-// Created by Leonid Zolotarev on 1/23/24.
-//
-
import SwiftUI
import SwiftyCrop
@@ -12,12 +5,24 @@ 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
- @State private var rotateImage: Bool = true
-
+ @State private var cropImageCircular: Bool
+ @State private var rotateImage: Bool
+ @State private var maxMagnificationScale: CGFloat
+ @State private var maskRadius: CGFloat
+ @FocusState private var textFieldFocused: Bool
+
+ init() {
+ let defaultConfiguration = SwiftyCropConfiguration()
+ _cropImageCircular = State(initialValue: defaultConfiguration.cropImageCircular)
+ _rotateImage = State(initialValue: defaultConfiguration.rotateImage)
+ _maxMagnificationScale = State(initialValue: defaultConfiguration.maxMagnificationScale)
+ _maskRadius = State(initialValue: defaultConfiguration.maskRadius)
+ }
+
var body: some View {
VStack {
Spacer()
+
Group {
if let selectedImage = selectedImage {
Image(uiImage: selectedImage)
@@ -30,34 +35,72 @@ struct ContentView: View {
}
.scaledToFit()
.padding()
+
Spacer()
+
GroupBox {
- HStack {
- Button {
- loadImage()
- } label: {
- LongText(title: "Load image")
+ VStack(spacing: 15) {
+ HStack {
+ Button {
+ loadImage()
+ } label: {
+ LongText(title: "Load image")
+ }
+ Button {
+ showImageCropper.toggle()
+ } label: {
+ LongText(title: "Crop image")
+ }
}
- Button {
- showImageCropper.toggle()
- } label: {
- LongText(title: "Crop image")
+
+ HStack {
+ Text("Mask shape")
+ .frame(maxWidth: .infinity, alignment: .leading)
+
+ Picker("maskShape", selection: $selectedShape) {
+ ForEach(MaskShape.allCases, id: \.self) { mask in
+ Text(String(describing: mask))
+ }
+ }
+ .pickerStyle(.segmented)
+ }
+
+ Toggle("Crop image to circle", isOn: $cropImageCircular)
+
+ Toggle("Rotate image", isOn: $rotateImage)
+
+ HStack {
+ Text("Max magnification")
+ .frame(maxWidth: .infinity, alignment: .leading)
+
+ DecimalTextField(value: $maxMagnificationScale)
+ .focused($textFieldFocused)
+ }
+
+ HStack {
+ Text("Mask radius")
+ .frame(maxWidth: .infinity, alignment: .leading)
+
+ Button {
+ maskRadius = UIScreen.main.bounds.width / 2
+ } label: {
+ Image(systemName: "arrow.up.left.and.arrow.down.right")
+ .font(.footnote)
+ }
+
+ DecimalTextField(value: $maskRadius)
+ .focused($textFieldFocused)
}
}
- HStack {
- ShapeButton(
- title: "Use square crop",
- shape: .square,
- selection: $selectedShape
- )
- ShapeButton(
- title: "Use circle crop",
- shape: .circle,
- selection: $selectedShape
- )
+ }
+ .toolbar {
+ ToolbarItemGroup(placement: .keyboard) {
+ Spacer()
+
+ Button("Done") {
+ textFieldFocused = false
+ }
}
- Toggle("Crop image to circle", isOn: $cropImageCircular)
- Toggle("Rotate image", isOn: $rotateImage)
}
.buttonStyle(.bordered)
.padding()
@@ -71,6 +114,8 @@ struct ContentView: View {
imageToCrop: selectedImage,
maskShape: selectedShape,
configuration: SwiftyCropConfiguration(
+ maxMagnificationScale: maxMagnificationScale,
+ maskRadius: maskRadius,
cropImageCircular: cropImageCircular,
rotateImage: rotateImage
)
@@ -81,13 +126,13 @@ struct ContentView: View {
}
}
}
-
+
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"
@@ -95,7 +140,7 @@ struct ContentView: View {
let (data, _) = try? await URLSession.shared.data(from: url),
let image = UIImage(data: data)
else { return nil }
-
+
return image
}
}