Skip to content

Commit 3e5d6a9

Browse files
authored
Merge pull request #38 from krayc425/krayc425/oncancel
Add onCancel completion callback
2 parents ddf6ad7 + fd1af9a commit 3e5d6a9

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

Demo/SwiftyCropDemo/ContentView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ struct ContentView: View {
174174
usesLiquidGlassDesign: usesLiquidGlassDesign,
175175
zoomSensitivity: zoomSensitivity,
176176
rectAspectRatio: rectAspectRatio.getValue()
177-
)
177+
),
178+
onCancel: {
179+
print("Operation cancelled")
180+
}
178181
) { croppedImage in
179182
// Do something with the returned, cropped image
180183
self.selectedImage = croppedImage

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ struct ExampleView: View {
142142
If you want to display `SwiftyCrop` inside a sheet, use `NavigationView` instead of `NavigationStack` in case you want to wrap it.
143143
```
144144

145-
SwiftyCrop supports two different mask shapes for cropping:
145+
SwiftyCrop supports three different mask shapes for cropping:
146146
- `circle`
147147
- `square`
148148
- `rectangle`

Sources/SwiftyCrop/SwiftyCrop.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@ import SwiftUI
88
/// - imageToCrop: The image to be cropped.
99
/// - maskShape: The shape of the mask used for cropping.
1010
/// - configuration: The configuration for the cropping behavior. If nothing is specified, the default is used.
11+
/// - onCancel: An optional closure that's called when the cropping is cancelled.
1112
/// - onComplete: A closure that's called when the cropping is complete. This closure returns the cropped `UIImage?`.
1213
/// If an error occurs the return value is nil.
1314
public struct SwiftyCropView: View {
1415
private let imageToCrop: UIImage
1516
private let maskShape: MaskShape
1617
private let configuration: SwiftyCropConfiguration
18+
private let onCancel: (() -> Void)?
1719
private let onComplete: (UIImage?) -> Void
1820

1921
public init(
2022
imageToCrop: UIImage,
2123
maskShape: MaskShape,
2224
configuration: SwiftyCropConfiguration = SwiftyCropConfiguration(),
25+
onCancel: (() -> Void)? = nil,
2326
onComplete: @escaping (UIImage?) -> Void
2427
) {
2528
self.imageToCrop = imageToCrop
2629
self.maskShape = maskShape
2730
self.configuration = configuration
31+
self.onCancel = onCancel
2832
self.onComplete = onComplete
2933
}
3034

@@ -33,6 +37,7 @@ public struct SwiftyCropView: View {
3337
image: imageToCrop,
3438
maskShape: maskShape,
3539
configuration: configuration,
40+
onCancel: onCancel,
3641
onComplete: onComplete
3742
)
3843
}

Sources/SwiftyCrop/View/CropView.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ struct CropView: View {
1010
private let image: UIImage
1111
private let maskShape: MaskShape
1212
private let configuration: SwiftyCropConfiguration
13+
private let onCancel: (() -> Void)?
1314
private let onComplete: (UIImage?) -> Void
1415
private let localizableTableName: String
1516

1617
init(
1718
image: UIImage,
1819
maskShape: MaskShape,
1920
configuration: SwiftyCropConfiguration,
21+
onCancel: (() -> Void)? = nil,
2022
onComplete: @escaping (UIImage?) -> Void
2123
) {
2224
self.image = image
2325
self.maskShape = maskShape
2426
self.configuration = configuration
27+
self.onCancel = onCancel
2528
self.onComplete = onComplete
2629
_viewModel = StateObject(
2730
wrappedValue: CropViewModel(
@@ -48,14 +51,17 @@ struct CropView: View {
4851
#endif
4952
}
5053

51-
@available (iOS 26, *)
54+
@available(iOS 26, *)
5255
private func buildLiquidGlassBody(configuration: SwiftyCropConfiguration) -> some View {
5356
ZStack {
5457
VStack {
5558
ToolbarView(
5659
viewModel: viewModel,
5760
configuration: configuration,
58-
dismiss: { dismiss() }
61+
dismiss: {
62+
onCancel?()
63+
dismiss()
64+
}
5965
) {
6066
await MainActor.run {
6167
isCropping = true
@@ -105,7 +111,10 @@ struct CropView: View {
105111
Legacy_ButtonsView(
106112
configuration: configuration,
107113
localizableTableName: localizableTableName,
108-
dismiss: { dismiss() }
114+
dismiss: {
115+
onCancel?()
116+
dismiss()
117+
}
109118
) {
110119
await MainActor.run {
111120
isCropping = true

0 commit comments

Comments
 (0)