Skip to content

Commit f3189b7

Browse files
authored
Merge pull request #17 from Festanny/master
Adding Rectangle (4:3) for cropping
2 parents 24a67bf + 186ad26 commit f3189b7

File tree

7 files changed

+231
-190
lines changed

7 files changed

+231
-190
lines changed

Demo/SwiftyCropDemo/ContentView.swift

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,29 @@ struct ContentView: View {
55
@State private var showImageCropper: Bool = false
66
@State private var selectedImage: UIImage?
77
@State private var selectedShape: MaskShape = .square
8+
@State private var rectAspectRatio: PresetAspectRatios = .fourToThree
89
@State private var cropImageCircular: Bool
910
@State private var rotateImage: Bool
1011
@State private var maxMagnificationScale: CGFloat
1112
@State private var maskRadius: CGFloat
1213
@State private var zoomSensitivity: CGFloat
1314
@FocusState private var textFieldFocused: Bool
1415

16+
enum PresetAspectRatios: String, CaseIterable {
17+
case fourToThree = "4:3"
18+
case sixteenToNine = "16:9"
19+
20+
func getValue() -> CGFloat {
21+
switch self {
22+
case .fourToThree:
23+
4/3
24+
25+
case .sixteenToNine:
26+
16/9
27+
}
28+
}
29+
}
30+
1531
init() {
1632
let defaultConfiguration = SwiftyCropConfiguration()
1733
_cropImageCircular = State(initialValue: defaultConfiguration.cropImageCircular)
@@ -59,14 +75,29 @@ struct ContentView: View {
5975
Text("Mask shape")
6076
.frame(maxWidth: .infinity, alignment: .leading)
6177

62-
Picker("maskShape", selection: $selectedShape) {
78+
Picker("maskShape", selection: $selectedShape.animation()) {
6379
ForEach(MaskShape.allCases, id: \.self) { mask in
6480
Text(String(describing: mask))
6581
}
6682
}
6783
.pickerStyle(.segmented)
6884
}
6985

86+
if selectedShape == .rectangle {
87+
HStack {
88+
Text("Rect aspect ratio")
89+
.frame(maxWidth: .infinity, alignment: .leading)
90+
91+
Picker("rectAspectRatio", selection: $rectAspectRatio) {
92+
ForEach(PresetAspectRatios.allCases, id: \.self) { aspectRatio in
93+
Text(aspectRatio.rawValue)
94+
}
95+
96+
}
97+
.pickerStyle(.segmented)
98+
}
99+
}
100+
70101
Toggle("Crop image to circle", isOn: $cropImageCircular)
71102

72103
Toggle("Rotate image", isOn: $rotateImage)
@@ -129,7 +160,8 @@ struct ContentView: View {
129160
maskRadius: maskRadius,
130161
cropImageCircular: cropImageCircular,
131162
rotateImage: rotateImage,
132-
zoomSensitivity: zoomSensitivity
163+
zoomSensitivity: zoomSensitivity,
164+
rectAspectRatio: rectAspectRatio.getValue()
133165
)
134166
) { croppedImage in
135167
// Do something with the returned, cropped image

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,9 @@ If you want to display `SwiftyCrop` inside a sheet, use `NavigationView` instead
138138
SwiftyCrop supports two different mask shapes for cropping:
139139
- `circle`
140140
- `square`
141+
- `rectangle`
141142

142-
This is only the shape of the mask the user will see when cropping the image. The resulting, cropped image will always be a square by default. You can override this using a configuration.
143+
This is only the shape of the mask the user will see when cropping the image. The resulting, cropped image will always be a square by default when using `circle` or `square`. To get a circular cropped image, you can override this using a configuration.
143144

144145
You can also configure `SwiftyCropView` by passing a `SwiftyCropConfiguration`. A configuration has the following properties:
145146

@@ -148,8 +149,9 @@ You can also configure `SwiftyCropView` by passing a `SwiftyCropConfiguration`.
148149
| `maxMagnificationScale` | `CGFloat`: The maximum scale factor that the image can be magnified while cropping. Defaults to `4.0`. |
149150
| `maskRadius` | `CGFloat`: The radius of the mask used for cropping. Defaults to `130`. A good way is to make it dependend on the screens size. |
150151
| `cropImageCircular` | `Bool`: When using the cropping mask `circle`, whether the resulting image should also be masked as circle. Defaults to `false`. |
151-
| `rotateImage` | `Bool`: Whether the image can be rotated when cropping using pinch gestures. Defaults to `true`. |
152+
| `rotateImage` | `Bool`: Whether the image can be rotated when cropping using pinch gestures. Defaults to `false`. |
152153
| `zoomSensitivity` | `CGFloat`: Zoom sensitivity when cropping. Increase to make zoom faster / less sensitive. Defaults to `1.0`. |
154+
| `rectAspectRatio` | `CGFloat`: The aspect ratio to use when a rectangular mask shape is used. Defaults to `4:3`. |
153155

154156
Create a configuration like this:
155157
```swift
@@ -158,7 +160,8 @@ let configuration = SwiftyCropConfiguration(
158160
maskRadius: 130,
159161
cropImageCircular: false,
160162
rotateImage: true,
161-
zoomSensitivity = 1.0
163+
zoomSensitivity = 1.0,
164+
rectAspectRatio = 4/3
162165
)
163166
```
164167
and use it like this:
@@ -185,6 +188,8 @@ Thanks to [@leoz](https://github.com/leoz) for adding the circular crop mode, th
185188

186189
Thanks to [@kevin-hv](https://github.com/kevin-hv) for adding the hungarian localization 🇭🇺
187190

191+
Thanks to [@Festanny](https://github.com/Festanny) for helping with the recangular cropping functionality 🎉
192+
188193
## ✍️ Author
189194

190195
Benedikt Betz & CHECK24

0 commit comments

Comments
 (0)