1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# SwiftyCrop - SwiftUI
[](https://github.com/benedom/SwiftyCrop/actions/workflows/build-swift.yml)



<a href="https://github.com/benedom/SwiftyCrop/blob/master/LICENSE.md">
<img src="https://img.shields.io/badge/License%20-%20MIT%20-%20blue" alt="License - MIT">
</a>
<p align="center">
<img src="https://github.com/benedom/SwiftyCrop/blob/master/Assets/demo.gif" style="margin: auto; width: 250px"/>
</p>
<p align="center">
<img src="https://github.com/benedom/SwiftyCrop/blob/master/Assets/crop_circle.png" style="margin: auto; width: 250px"/>
<img src="https://github.com/benedom/SwiftyCrop/blob/master/Assets/crop_square.png" style="margin: auto; width: 250px"/>
</p>
## 🔭 Overview
SwiftyCrop allows users to seamlessly crop images within their SwiftUI applications. It provides a user-friendly interface that makes cropping an image as simple as selecting the desired area.
With SwiftyCrop, you can easily adjust the cropping area, maintain aspect ratio, zoom in and out for precise cropping. You can also specify the cropping mask to be a square or circle.
The following languages are supported & localized:
- 🇬🇧 English
- 🇩🇪 German
- 🇫🇷 French
- 🇮🇹 Italian
- 🇷🇺 Russian
- 🇪🇸 Spanish
- 🇹🇷 Turkish
- 🇺🇦 Ukrainian
- 🇭🇺 Hungarian
The localization file can be found in `Sources/SwiftyCrop/Resources`.
## 📕 Contents
- [Requirements](#-requirements)
- [Installation](#-installation)
- [Demo App](#📱-demo-app)
- [Usage](#-usage)
- [Contributors](#-contributors)
- [Author](#-author)
- [License](#-license)
## 🧳 Requirements
- iOS 16.0 or later
- Xcode 15.0 or later
- Swift 5.9 or later
## 💻 Installation
There are two ways to use SwiftyCrop in your project:
- using Swift Package Manager
- manual install (embed Xcode Project)
### Swift Package Manager
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for managing the distribution of Swift code. It’s integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies.
To integrate `SwiftyCrop` into your Xcode project using Xcode 15.0 or later, specify it in `File > Swift Packages > Add Package Dependency...`:
```ogdl
https://github.com/benedom/SwiftyCrop
```
### Manually
If you prefer not to use any of dependency managers, you can integrate `SwiftyCrop` into your project manually. Put `Sources/SwiftyCrop` folder in your Xcode project. Make sure to enable `Copy items if needed` and `Create groups`.
## 📱 Demo App
To get a feeling how `SwiftyCropView` works you can run the demo app (thanks to [@leoz](https://github.com/leoz)).
## 🛠️ Usage
### Quick Start
This example shows how to display `SwiftyCropView` in a full screen cover after an image has been set.
```swift
import SwiftUI
import SwiftyCrop
struct ExampleView: View {
@State private var showImageCropper: Bool = false
@State private var selectedImage: UIImage?
var body: some View {
VStack {
/*
Update `selectedImage` with the image you want to crop,
e.g. after picking it from the library or downloading it.
As soon as you have done this, toggle `showImageCropper`.
Below is a sample implementation:
*/
Button("Crop downloaded image") {
Task {
selectedImage = await downloadExampleImage()
showImageCropper.toggle()
}
}
}
.fullScreenCover(isPresented: $showImageCropper) {
if let selectedImage = selectedImage {
SwiftyCropView(
imageToCrop: selectedImage,
maskShape: .square
) { croppedImage in
// Do something with the returned, cropped image
}
}
}
}
// Example function for downloading an image
private func downloadExampleImage() async -> UIImage? {
let urlString = "https://picsum.photos/1000/1200"
guard let url = URL(string: urlString),
let (data, _) = try? await URLSession.shared.data(from: url),
let image = UIImage(data: data)
else { return nil }
return image
}
}
```
SwiftyCrop supports two different mask shapes for cropping:
- `circle`
- `square`
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.
You can also configure `SwiftyCropView` by passing a `SwiftyCropConfiguration`. A configuration has the following properties:
| Property | Description |
| ----------- | ----------- |
| `maxMagnificationScale` | `CGFloat`: The maximum scale factor that the image can be magnified while cropping. Defaults to `4.0`. |
| `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. |
| `cropImageCircular` | `Bool`: When using the cropping mask `circle`, whether the resulting image should also be masked as circle. Defaults to `false`. |
| `rotateImage` | `Bool`: Whether the image can be rotated when cropping using pinch gestures. Defaults to `true`. |
| `zoomSensitivity` | `CGFloat`: Zoom sensitivity when cropping. Increase to make zoom faster / less sensitive. Defaults to `1.0`. |
Create a configuration like this:
```swift
let configuration = SwiftyCropConfiguration(
maxMagnificationScale = 4.0,
maskRadius: 130,
cropImageCircular: false,
rotateImage: true,
zoomSensitivity = 1.0
)
```
and use it like this:
```swift
.fullScreenCover(isPresented: $showImageCropper) {
if let selectedImage = selectedImage {
SwiftyCropView(
imageToCrop: selectedImage,
maskShape: .square,
// Use the configuration
configuration: configuration
) { croppedImage in
// Do something with the returned, cropped image
}
}
}
```
## 👨💻 Contributors
All issue reports, feature requests, pull requests and GitHub stars are welcomed and much appreciated.
Thanks to [@leoz](https://github.com/leoz) for adding the circular crop mode, the demo app and the rotation functionality 🎉
Thanks to [@kevin-hv](https://github.com/kevin-hv) for adding the hungarian localization 🇭🇺
## ✍️ Author
Benedikt Betz & CHECK24
## 📃 License
`SwiftyCrop` is available under the MIT license. See the [LICENSE](https://github.com/benedom/SwiftyCrop/blob/master/LICENSE.md) file for more info.
|