Skip to content

Commit 8098d9b

Browse files
committed
Add initial crop rect param
1 parent 7934f71 commit 8098d9b

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

Demo-macOS/Demo-macOS/ContentView.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ import ImageCropper
1111
struct ContentView: View {
1212

1313
@State var ratio: CropperRatio = CropperRatio.r_1_1
14+
@State var cropRect: CGRect? = nil
1415

1516
var body: some View {
1617
VStack {
1718
Text("Image Cropper Demo")
1819

1920
Spacer()
2021

21-
ImageCropperView(image:
22-
Image("stock"),
23-
ratio: ratio
24-
)
22+
ImageCropperView(image: Image("stock"),
23+
cropRect: cropRect,
24+
ratio: ratio)
2525
.onCropChanged { (newCrop) in
26-
print(newCrop)
26+
cropRect = newCrop
2727
}
2828

2929

@@ -34,18 +34,21 @@ struct ContentView: View {
3434

3535
Button(action: {
3636
ratio = CropperRatio.r_1_1
37+
cropRect = nil
3738
}) {
3839
Text("1:1")
3940
}
4041

4142
Button(action: {
4243
ratio = CropperRatio.r_3_2
44+
cropRect = nil
4345
}) {
4446
Text("3:2")
4547
}
4648

4749
Button(action: {
4850
ratio = CropperRatio.r_4_3
51+
cropRect = nil
4952
}) {
5053
Text("4:3")
5154
}

Sources/ImageCropper/CropperView.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,16 @@ final class CropperViewModel: ObservableObject {
103103
var onCropChanged : ((CGRect) -> Void)?
104104

105105
init(parentProxy: GeometryProxy,
106-
initialCropRect: CGRect? = nil,
106+
cropRect: CGRect? = nil,
107107
ratio: CropperRatio,
108108
onCropChanged : ((CGRect) -> Void)? = nil) {
109109

110110
self.onCropChanged = onCropChanged
111111
self.parentProxy = parentProxy
112112
self.ratio = ratio
113113

114-
if let initialCropRect = initialCropRect {
115-
self.cropperRect = cropRectToCropView(with: initialCropRect)
114+
if let cropRect = cropRect {
115+
self.cropperRect = cropRectToCropView(with: cropRect)
116116
}
117117
else {
118118
initializeCropRect()
@@ -123,7 +123,10 @@ final class CropperViewModel: ObservableObject {
123123
cropperRect = createCropperFrame(for: parentProxy.size)
124124

125125
// Save initial crop
126-
onCropChanged?(cropViewToCropRect())
126+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
127+
self.onCropChanged?(self.cropViewToCropRect())
128+
}
129+
127130
}
128131

129132
fileprivate func cropViewToCropRect() -> CGRect {

Sources/ImageCropper/ImageCropperView.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
import SwiftUI
99

1010
public struct ImageCropperView: View {
11-
12-
var ratio: CropperRatio
13-
11+
1412
let image: Image
13+
let ratio: CropperRatio
14+
let cropRect: CGRect?
15+
1516
var onCropChanged : ((CGRect) -> Void)?
1617

1718
public init(image: Image,
19+
cropRect: CGRect? = nil,
1820
ratio: CropperRatio) {
1921
self.image = image
2022
self.ratio = ratio
23+
self.cropRect = cropRect
2124
}
2225

2326
public var body: some View {
@@ -27,7 +30,7 @@ public struct ImageCropperView: View {
2730
.overlay(
2831
GeometryReader { reader in
2932
CropperView(viewModel: CropperViewModel(parentProxy: reader,
30-
initialCropRect: nil,
33+
cropRect: cropRect,
3134
ratio: ratio,
3235
onCropChanged: onCropChanged))
3336
}

0 commit comments

Comments
 (0)