File tree Expand file tree Collapse file tree 3 files changed +22
-13
lines changed Expand file tree Collapse file tree 3 files changed +22
-13
lines changed Original file line number Diff line number Diff line change @@ -11,19 +11,19 @@ import ImageCropper
11
11
struct ContentView : View {
12
12
13
13
@State var ratio : CropperRatio = CropperRatio . r_1_1
14
+ @State var cropRect : CGRect ? = nil
14
15
15
16
var body : some View {
16
17
VStack {
17
18
Text ( " Image Cropper Demo " )
18
19
19
20
Spacer ( )
20
21
21
- ImageCropperView ( image:
22
- Image ( " stock " ) ,
23
- ratio: ratio
24
- )
22
+ ImageCropperView ( image: Image ( " stock " ) ,
23
+ cropRect: cropRect,
24
+ ratio: ratio)
25
25
. onCropChanged { ( newCrop) in
26
- print ( newCrop)
26
+ cropRect = newCrop
27
27
}
28
28
29
29
@@ -34,18 +34,21 @@ struct ContentView: View {
34
34
35
35
Button ( action: {
36
36
ratio = CropperRatio . r_1_1
37
+ cropRect = nil
37
38
} ) {
38
39
Text ( " 1:1 " )
39
40
}
40
41
41
42
Button ( action: {
42
43
ratio = CropperRatio . r_3_2
44
+ cropRect = nil
43
45
} ) {
44
46
Text ( " 3:2 " )
45
47
}
46
48
47
49
Button ( action: {
48
50
ratio = CropperRatio . r_4_3
51
+ cropRect = nil
49
52
} ) {
50
53
Text ( " 4:3 " )
51
54
}
Original file line number Diff line number Diff line change @@ -103,16 +103,16 @@ final class CropperViewModel: ObservableObject {
103
103
var onCropChanged : ( ( CGRect ) -> Void ) ?
104
104
105
105
init ( parentProxy: GeometryProxy ,
106
- initialCropRect : CGRect ? = nil ,
106
+ cropRect : CGRect ? = nil ,
107
107
ratio: CropperRatio ,
108
108
onCropChanged : ( ( CGRect ) -> Void ) ? = nil ) {
109
109
110
110
self . onCropChanged = onCropChanged
111
111
self . parentProxy = parentProxy
112
112
self . ratio = ratio
113
113
114
- if let initialCropRect = initialCropRect {
115
- self . cropperRect = cropRectToCropView ( with: initialCropRect )
114
+ if let cropRect = cropRect {
115
+ self . cropperRect = cropRectToCropView ( with: cropRect )
116
116
}
117
117
else {
118
118
initializeCropRect ( )
@@ -123,7 +123,10 @@ final class CropperViewModel: ObservableObject {
123
123
cropperRect = createCropperFrame ( for: parentProxy. size)
124
124
125
125
// Save initial crop
126
- onCropChanged ? ( cropViewToCropRect ( ) )
126
+ DispatchQueue . main. asyncAfter ( deadline: DispatchTime . now ( ) + 0.5 ) {
127
+ self . onCropChanged ? ( self . cropViewToCropRect ( ) )
128
+ }
129
+
127
130
}
128
131
129
132
fileprivate func cropViewToCropRect( ) -> CGRect {
Original file line number Diff line number Diff line change 8
8
import SwiftUI
9
9
10
10
public struct ImageCropperView : View {
11
-
12
- var ratio : CropperRatio
13
-
11
+
14
12
let image : Image
13
+ let ratio : CropperRatio
14
+ let cropRect : CGRect ?
15
+
15
16
var onCropChanged : ( ( CGRect ) -> Void ) ?
16
17
17
18
public init ( image: Image ,
19
+ cropRect: CGRect ? = nil ,
18
20
ratio: CropperRatio ) {
19
21
self . image = image
20
22
self . ratio = ratio
23
+ self . cropRect = cropRect
21
24
}
22
25
23
26
public var body : some View {
@@ -27,7 +30,7 @@ public struct ImageCropperView: View {
27
30
. overlay (
28
31
GeometryReader { reader in
29
32
CropperView ( viewModel: CropperViewModel ( parentProxy: reader,
30
- initialCropRect : nil ,
33
+ cropRect : cropRect ,
31
34
ratio: ratio,
32
35
onCropChanged: onCropChanged) )
33
36
}
You can’t perform that action at this time.
0 commit comments