-
Notifications
You must be signed in to change notification settings - Fork 0
/
CroppedImage.swift
328 lines (258 loc) · 9.09 KB
/
CroppedImage.swift
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//
// CroppedImage.swift
// watchthebirdie
//
// Created by Andrey Zarembo-Godzyatskiy on 31.08.2023.
//
import Foundation
import UIKit
//import Kingfisher
import SwiftUI
enum ViewMode {
case view
case edit
case crop
}
final class CroppedImageViewController: UIViewController, UIScrollViewDelegate, UIGestureRecognizerDelegate, CropTouchPanelDelegate {
let id: UUID
let imageURL: URL
let originalURL: URL
var angle: Angle {
didSet {
self.setupImageView()
}
}
var rotation: RotateLargeAnge {
didSet {
self.setupImageView()
}
}
var cropSize: CGSize {
didSet {
self.setupImageView()
}
}
var cropCenter: CGPoint {
didSet {
self.setupImageView()
}
}
let originalImageSize: CGSize
let edit_mode_edge: CGFloat = 96
let view_mode_edge: CGFloat = 0
let minCropSize: CGFloat = 128
var scrollView: UIScrollView = UIScrollView(frame: CGRect())
var containerView: UIView = UIView(frame: CGRect())
var imageView: UIImageView = UIImageView(frame: CGRect())
var touchPanel: CropTouchPanel = CropTouchPanel(frame: CGRect())
var cropView: UIView = UIView(frame: CGRect())
var cropCenterView = UIView(frame: CGRect())
var imageCenterView = UIView(frame: CGRect())
var previousConstraints: [NSLayoutConstraint] = []
var coordinator: CroppedImageCoordinator? = nil
var debug: Bool = true
var reportZoomChange: Bool = false
var zoom: CGFloat = 1 {
didSet {
self.scrollView.zoomScale = self.zoom
}
}
var mode: ViewMode = .crop {
didSet {
loadImage()
}
}
init(
id: UUID,
imageURL: URL,
originalURL: URL,
angle: Angle,
rotation: RotateLargeAnge,
cropSize: CGSize,
cropCenter: CGPoint,
originalImageSize: CGSize
) {
self.id = id
self.imageURL = imageURL
self.originalURL = originalURL
self.angle = angle
self.rotation = rotation
self.cropSize = cropSize
self.cropCenter = cropCenter
self.originalImageSize = originalImageSize
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.alpha = 0
scrollView.minimumZoomScale = 0.1
scrollView.maximumZoomScale = 10
scrollView.delegate = self
self.setupDebug()
self.view.addAndPin(self.scrollView)
self.view.addAndPin(self.touchPanel)
self.touchPanel.delegate = self
scrollView.addAndPin(containerView)
self.imageView.translatesAutoresizingMaskIntoConstraints = false
self.containerView.addSubview(self.imageView)
cropView.translatesAutoresizingMaskIntoConstraints = false
self.containerView.addSubview(cropView)
loadImage()
}
func loadImage() {
let url: URL
if mode == .view {
url = imageURL
} else {
url = originalURL
}
let data = try! Data(contentsOf: url)
self.imageView.image = UIImage(data: data)
self.setupImageView()
self.refreshOffsetAndZoom()
}
func setupImageView() {
NSLayoutConstraint.deactivate(previousConstraints)
previousConstraints = []
let imageSize = originalImageSize
let angle: Angle
if mode == .view {
angle = Angle()
}
else {
angle = Angle(radians: rotation.fullAngle(self.angle.radians))
}
let len = sqrt(pow(imageSize.width, 2) + pow(imageSize.height, 2))
let cropZoom = CGAffineTransform(scaleX: 1 /*self.zoom*/, y: 1 /*self.zoom*/)
let cropScale = CGAffineTransform(scaleX: len, y: len).concatenating(cropZoom)
let cropTransform = cropScale.rotated(by: angle.radians)
let cropCenterShift: CGAffineTransform
if mode == .edit {
cropCenterShift = CGAffineTransform.identity
}
else {
cropCenterShift = CGAffineTransform(translationX: len / 2, y: len / 2 )
}
let tSize = cropSize.applying(cropScale)
let tCrop = cropCenter.applying(cropTransform).applying(cropCenterShift)
let containerWidth: CGFloat
let containerHeight: CGFloat
if mode == .crop {
containerWidth = len
containerHeight = len
}
else {
containerWidth = tSize.width
containerHeight = tSize.height
}
let cw = containerView.widthAnchor.constraint(equalToConstant: containerWidth)
let ch = containerView.heightAnchor.constraint(equalToConstant: containerHeight)
previousConstraints.append(cw)
previousConstraints.append(ch)
cw.isActive = true
ch.isActive = true
let imageViewShiftX: CGFloat
let imageViewShiftY: CGFloat
if mode == .crop || mode == .view {
imageViewShiftX = 0
imageViewShiftY = 0
}
else {
imageViewShiftX = -tCrop.x
imageViewShiftY = -tCrop.y
print("tCrop \(tCrop)")
}
let icx = imageView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor, constant: imageViewShiftX)
let icy = imageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor, constant: imageViewShiftY)
previousConstraints.append(icx)
previousConstraints.append(icy)
icx.isActive = true
icy.isActive = true
imageView.transform = CGAffineTransform(rotationAngle: angle.radians)
imageCenterView.center = CGPoint(
x: imageView.image?.size.width ?? 0 / 2,
y: imageView.image?.size.height ?? 0 / 2)
cropView.bounds = CGRect(
x: 0,
y: 0,
width: tSize.width,
height: tSize.height)
let cropViewCenterShiftX: CGFloat
let cropViewCenterShiftY: CGFloat
if mode == .crop {
cropViewCenterShiftX = tCrop.x
cropViewCenterShiftY = tCrop.y
}
else {
cropViewCenterShiftX = containerWidth/2
cropViewCenterShiftY = containerHeight/2
}
cropView.center = CGPoint(
x: cropViewCenterShiftX,
y: cropViewCenterShiftY
)
cropCenterView.center = CGPoint(
x: tSize.width/2,
y: tSize.height/2)
}
func refreshOffsetAndZoom() {
let imageSize = originalImageSize
let len = sqrt(pow(imageSize.width, 2) + pow(imageSize.height, 2))
self.view.layoutIfNeeded()
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: .milliseconds(5)), execute: {
if self.mode == .crop {
self.zoom = self.view.frame.width / len
}
else {
self.zoom = self.view.frame.width / (len * self.cropSize.width)
}
self.updateOffsets()
self.view.alpha = 1
})
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return containerView
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
updateOffsets()
if self.reportZoomChange {
self.coordinator?.zoom?.wrappedValue = scrollView.zoomScale
}
updateCenterOffset()
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateCenterOffset()
}
func updateCenterOffset() {
let containerCenter: CGPoint = self.view.convert(containerView.center, from: scrollView.coordinateSpace)
let viewCenter: CGPoint = self.view.center
let shift: CGPoint = CGPoint(x: containerCenter.x - viewCenter.x,
y: containerCenter.y - viewCenter.y
)
Task {
self.coordinator?.centerShift?.wrappedValue = shift
// self.coordinator?.centerShift?.update()
}
// print("\(scrollView.contentOffset)")
}
func updateOffsets() {
let edge: CGFloat = view_mode_edge
let offsetX = max((scrollView.bounds.width - scrollView.contentSize.width) * 0.5, edge)
let offsetY = max((scrollView.bounds.height - scrollView.contentSize.height) * 0.5, edge)
scrollView.contentInset = UIEdgeInsets(
top: offsetY,
left: offsetX,
bottom: offsetY,
right: offsetX
)
updateCenterOffset()
}
}
struct ViewController_Preview: PreviewProvider {
static var previews: some View {
CroppedImageDebug()
}
}