-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathCropViewController.swift
More file actions
710 lines (602 loc) · 29.1 KB
/
CropViewController.swift
File metadata and controls
710 lines (602 loc) · 29.1 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//
// CropViewController.swift
//
// Copyright 2017-2025 Timothy Oliver. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#if canImport(TOCropViewController)
import TOCropViewController
#endif
/**
An enum containing all of the aspect ratio presets that this view controller supports
*/
public typealias CropViewControllerAspectRatioPreset = TOCropViewControllerAspectRatioPreset
/**
An enum denoting whether the control tool bar is drawn at the top, or the bottom of the screen in portrait mode
*/
public typealias CropViewControllerToolbarPosition = TOCropViewControllerToolbarPosition
/**
The type of cropping style for this view controller (ie a square or a circle cropping region)
*/
public typealias CropViewCroppingStyle = TOCropViewCroppingStyle
// ------------------------------------------------
/// @name Delegate
// ------------------------------------------------
@MainActor @objc public protocol CropViewControllerDelegate: NSObjectProtocol {
/**
Called when the user hits the Done button.
*/
@objc optional func cropViewControllerDidTapDone(_ cropViewController: CropViewController)
/**
Called when the user has committed the crop action, and provides
just the cropping rectangle.
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
@objc optional func cropViewController(_ cropViewController: CropViewController, didCropImageToRect cropRect: CGRect, angle: Int)
/**
Called when the user has committed the crop action, and provides
both the original image with crop co-ordinates.
@param image The newly cropped image.
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
@objc optional func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int)
/**
If the cropping style is set to circular, implementing this delegate will return a circle-cropped version of the selected
image, as well as it's cropping co-ordinates
@param image The newly cropped image, clipped to a circle shape
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
@objc optional func cropViewController(_ cropViewController: CropViewController, didCropToCircularImage image: UIImage, withRect cropRect: CGRect, angle: Int)
/**
If implemented, when the user hits cancel, or completes a
UIActivityViewController operation, this delegate will be called,
giving you a chance to manually dismiss the view controller
@param cancelled Whether a cropping action was actually performed, or if the user explicitly hit 'Cancel'
*/
@objc optional func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool)
}
// ------------------------------------------------
/// @name Class
// ------------------------------------------------
open class CropViewController: UIViewController, TOCropViewControllerDelegate {
/**
The original, uncropped image that was passed to this controller.
*/
public var image: UIImage { return self.toCropViewController.image }
/**
The minimum croping aspect ratio. If set, user is prevented from
setting cropping rectangle to lower aspect ratio than defined by the parameter.
*/
public var minimumAspectRatio: CGFloat {
set { toCropViewController.minimumAspectRatio = newValue }
get { return toCropViewController.minimumAspectRatio }
}
/**
The view controller's delegate that will receive the resulting
cropped image, as well as crop information.
*/
public weak var delegate: (any CropViewControllerDelegate)? {
didSet { self.setUpDelegateHandlers() }
}
/**
Set the title text that appears at the top of the view controller
*/
override open var title: String? {
set { toCropViewController.title = newValue }
get { return toCropViewController.title }
}
/**
If true, when the user hits 'Done', a UIActivityController will appear
before the view controller ends.
*/
public var showActivitySheetOnDone: Bool {
set { toCropViewController.showActivitySheetOnDone = newValue }
get { return toCropViewController.showActivitySheetOnDone }
}
/**
In the coordinate space of the image itself, the region that is currently
being highlighted by the crop box.
This property can be set before the controller is presented to have
the image 'restored' to a previous cropping layout.
*/
public var imageCropFrame: CGRect {
set { toCropViewController.imageCropFrame = newValue }
get { return toCropViewController.imageCropFrame }
}
/**
The angle in which the image is rotated in the crop view.
This can only be in 90 degree increments (eg, 0, 90, 180, 270).
This property can be set before the controller is presented to have
the image 'restored' to a previous cropping layout.
*/
public var angle: Int {
set { toCropViewController.angle = newValue }
get { return toCropViewController.angle }
}
/**
The cropping style of this particular crop view controller
*/
public var croppingStyle: CropViewCroppingStyle {
return toCropViewController.croppingStyle
}
/**
A choice from one of the pre-defined aspect ratio presets
*/
public var aspectRatioPreset: CGSize {
set { toCropViewController.aspectRatioPreset = newValue }
get { return toCropViewController.aspectRatioPreset }
}
/**
Title label which can be used to show instruction on the top of the crop view controller
*/
public var titleLabel: UILabel? {
return toCropViewController.titleLabel
}
/**
If true, while it can still be resized, the crop box will be locked to its current aspect ratio.
If this is set to YES, and `resetAspectRatioEnabled` is set to NO, then the aspect ratio
button will automatically be hidden from the toolbar.
Default is false.
*/
public var aspectRatioLockEnabled: Bool {
set { toCropViewController.aspectRatioLockEnabled = newValue }
get { return toCropViewController.aspectRatioLockEnabled }
}
/**
If true, a custom aspect ratio is set, and the aspectRatioLockEnabled is set to true, the crop box will swap it's dimensions depending on portrait or landscape sized images. This value also controls whether the dimensions can swap when the image is rotated.
Default is false.
*/
public var aspectRatioLockDimensionSwapEnabled: Bool {
set { toCropViewController.aspectRatioLockDimensionSwapEnabled = newValue }
get { return toCropViewController.aspectRatioLockDimensionSwapEnabled }
}
/**
If true, tapping the reset button will also reset the aspect ratio back to the image
default ratio. Otherwise, the reset will just zoom out to the current aspect ratio.
If this is set to false, and `aspectRatioLockEnabled` is set to YES, then the aspect ratio
button will automatically be hidden from the toolbar.
Default is true
*/
public var resetAspectRatioEnabled: Bool {
set { toCropViewController.resetAspectRatioEnabled = newValue }
get { return toCropViewController.resetAspectRatioEnabled }
}
/**
The position of the Toolbar the default value is `TOCropViewControllerToolbarPositionBottom`.
*/
public var toolbarPosition: CropViewControllerToolbarPosition {
set { toCropViewController.toolbarPosition = newValue }
get { return toCropViewController.toolbarPosition }
}
/**
When disabled, an additional rotation button that rotates the canvas in
90-degree segments in a clockwise direction is shown in the toolbar.
Default is false.
*/
public var rotateClockwiseButtonHidden: Bool {
set { toCropViewController.rotateClockwiseButtonHidden = newValue }
get { return toCropViewController.rotateClockwiseButtonHidden }
}
/**
When enabled, hides the rotation button, as well as the alternative rotation
button visible when `showClockwiseRotationButton` is set to true.
Default is false.
*/
public var rotateButtonsHidden: Bool {
set { toCropViewController.rotateButtonsHidden = newValue }
get { return toCropViewController.rotateButtonsHidden }
}
/**
When enabled, hides the 'Reset' button on the toolbar.
Default is false.
*/
public var resetButtonHidden: Bool {
set { toCropViewController.resetButtonHidden = newValue }
get { return toCropViewController.resetButtonHidden }
}
/**
When enabled, hides the 'Aspect Ratio Picker' button on the toolbar.
Default is false.
*/
public var aspectRatioPickerButtonHidden: Bool {
set { toCropViewController.aspectRatioPickerButtonHidden = newValue }
get { return toCropViewController.aspectRatioPickerButtonHidden }
}
/**
When enabled, hides the 'Done' button on the toolbar.
Default is false.
*/
public var doneButtonHidden: Bool {
set { toCropViewController.doneButtonHidden = newValue }
get { return toCropViewController.doneButtonHidden }
}
/**
When enabled, hides the 'Cancel' button on the toolbar.
Default is false.
*/
public var cancelButtonHidden: Bool {
set { toCropViewController.cancelButtonHidden = newValue }
get { return toCropViewController.cancelButtonHidden }
}
/**
If `showActivitySheetOnDone` is true, then these activity items will
be supplied to that UIActivityViewController in addition to the
`TOActivityCroppedImageProvider` object.
*/
public var activityItems: [Any]? {
set { toCropViewController.activityItems = newValue }
get { return toCropViewController.activityItems }
}
/**
If `showActivitySheetOnDone` is true, then you may specify any
custom activities your app implements in this array. If your activity requires
access to the cropping information, it can be accessed in the supplied
`TOActivityCroppedImageProvider` object
*/
public var applicationActivities: [UIActivity]? {
set { toCropViewController.applicationActivities = newValue }
get { return toCropViewController.applicationActivities }
}
/**
If `showActivitySheetOnDone` is true, then you may expliclty
set activities that won't appear in the share sheet here.
*/
public var excludedActivityTypes: [UIActivity.ActivityType]? {
set { toCropViewController.excludedActivityTypes = newValue }
get { return toCropViewController.excludedActivityTypes }
}
/**
An array of `TOCropViewControllerAspectRatioPreset` enum values denoting which
aspect ratios the crop view controller may display (Default is nil. All are shown)
*/
public var allowedAspectRatios: [CropViewControllerAspectRatioPreset]? {
set { toCropViewController.allowedAspectRatios = newValue }
get { return toCropViewController.allowedAspectRatios }
}
/**
Called when the user hits the Done button.
*/
public var onDidTapDone: (() -> Void)? {
set { toCropViewController.onDidTapDone = newValue }
get { return toCropViewController.onDidTapDone }
}
/**
When the user hits cancel, or completes a
UIActivityViewController operation, this block will be called,
giving you a chance to manually dismiss the view controller
*/
public var onDidFinishCancelled: ((Bool) -> (Void))? {
set { toCropViewController.onDidFinishCancelled = newValue }
get { return toCropViewController.onDidFinishCancelled }
}
/**
Called when the user has committed the crop action, and provides
just the cropping rectangle.
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
public var onDidCropImageToRect: ((CGRect, Int) -> (Void))? {
set { toCropViewController.onDidCropImageToRect = newValue }
get { return toCropViewController.onDidCropImageToRect }
}
/**
Called when the user has committed the crop action, and provides
both the cropped image with crop co-ordinates.
@param image The newly cropped image.
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
public var onDidCropToRect: ((UIImage, CGRect, NSInteger) -> (Void))? {
set { toCropViewController.onDidCropToRect = newValue }
get { return toCropViewController.onDidCropToRect }
}
/**
If the cropping style is set to circular, this block will return a circle-cropped version of the selected
image, as well as it's cropping co-ordinates
@param image The newly cropped image, clipped to a circle shape
@param cropRect A rectangle indicating the crop region of the image the user chose (In the original image's local co-ordinate space)
@param angle The angle of the image when it was cropped
*/
public var onDidCropToCircleImage: ((UIImage, CGRect, NSInteger) -> (Void))? {
set { toCropViewController.onDidCropToCircleImage = newValue }
get { return toCropViewController.onDidCropToCircleImage }
}
/**
The crop view managed by this view controller.
*/
public var cropView: TOCropView {
return toCropViewController.cropView
}
/**
The toolbar managed by this view controller.
*/
public var toolbar: TOCropToolbar {
return toCropViewController.toolbar
}
/*
If this controller is embedded in UINavigationController its navigation bar is hidden by default. Set this property to false to show the navigation bar. This must be set before this controller is presented.
*/
public var hidesNavigationBar: Bool {
set { toCropViewController.hidesNavigationBar = newValue }
get { return toCropViewController.hidesNavigationBar }
}
/**
Title for the 'Done' button.
Setting this will override the Default which is a localized string for "Done".
*/
public var doneButtonTitle: String! {
set { toCropViewController.doneButtonTitle = newValue }
get { return toCropViewController.doneButtonTitle }
}
/**
Title for the 'Cancel' button.
Setting this will override the Default which is a localized string for "Cancel".
*/
public var cancelButtonTitle: String! {
set { toCropViewController.cancelButtonTitle = newValue }
get { return toCropViewController.cancelButtonTitle }
}
/**
If true, button icons are visible in portairt instead button text.
Default is NO.
*/
public var showOnlyIcons: Bool {
set { toCropViewController.showOnlyIcons = newValue }
get { return toCropViewController.showOnlyIcons }
}
/**
Shows a confirmation dialog when the user hits 'Cancel' and there are pending changes.
(Default is NO)
*/
public var showCancelConfirmationDialog: Bool {
set { toCropViewController.showCancelConfirmationDialog = newValue }
get { return toCropViewController.showCancelConfirmationDialog }
}
/**
Color for the 'Done' button.
Setting this will override the default color.
*/
public var doneButtonColor: UIColor? {
set { toCropViewController.doneButtonColor = newValue }
get { return toCropViewController.doneButtonColor }
}
/**
Color for the 'Cancel' button.
Setting this will override the default color.
*/
public var cancelButtonColor: UIColor? {
set { toCropViewController.cancelButtonColor = newValue }
get { return toCropViewController.cancelButtonColor }
}
/**
A computed property to get or set the reverse layout on toolbar.
By setting this property, you can control the direction in which the toolbar is laid out.
Default is NO.
*/
public var reverseContentLayout: Bool {
set { toCropViewController.reverseContentLayout = newValue }
get { toCropViewController.reverseContentLayout }
}
/**
This class internally manages and abstracts access to a `TOCropViewController` instance
:nodoc:
*/
internal let toCropViewController: TOCropViewController!
/**
Forward status bar status style changes to the crop view controller
:nodoc:
*/
open override var childForStatusBarStyle: UIViewController? {
return toCropViewController
}
/**
Forward status bar status visibility changes to the crop view controller
:nodoc:
*/
open override var childForStatusBarHidden: UIViewController? {
return toCropViewController
}
open override var prefersStatusBarHidden: Bool {
return false
}
open override var preferredStatusBarStyle: UIStatusBarStyle {
return toCropViewController.preferredStatusBarStyle
}
open override var preferredScreenEdgesDeferringSystemGestures: UIRectEdge {
return toCropViewController.preferredScreenEdgesDeferringSystemGestures
}
// ------------------------------------------------
/// @name Object Creation
// ------------------------------------------------
/**
Creates a new instance of a crop view controller with the supplied image
@param image The image that will be used to crop.
*/
public init(image: UIImage) {
self.toCropViewController = TOCropViewController(image: image)
super.init(nibName: nil, bundle: nil)
setUpCropController()
}
/**
Creates a new instance of a crop view controller with the supplied image and cropping style
@param style The cropping style that will be used with this view controller (eg, rectangular, or circular)
@param image The image that will be cropped
*/
public init(croppingStyle: CropViewCroppingStyle, image: UIImage) {
self.toCropViewController = TOCropViewController(croppingStyle: croppingStyle, image: image)
super.init(nibName: nil, bundle: nil)
setUpCropController()
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Defer adding the view until we're about to be presented
if toCropViewController.view.superview == nil {
view.addSubview(toCropViewController.view)
}
}
open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
toCropViewController.view.frame = view.bounds
toCropViewController.viewDidLayoutSubviews()
}
/**
Commits the crop action as if user pressed done button in the bottom bar themself
*/
public func commitCurrentCrop() {
toCropViewController.commitCurrentCrop()
}
/**
Resets object of TOCropViewController class as if user pressed reset button in the bottom bar themself
*/
public func resetCropViewLayout() {
toCropViewController.resetCropViewLayout()
}
/**
Set the aspect ratio to be one of the available preset options. These presets have specific behaviour
such as swapping their dimensions depending on portrait or landscape sized images.
@param aspectRatioPreset The aspect ratio preset
@param animated Whether the transition to the aspect ratio is animated
*/
public func setAspectRatioPreset(_ aspectRatio: CGSize, animated: Bool) {
toCropViewController.setAspectRatioPreset(aspectRatio, animated: animated)
}
/**
Play a custom animation of the target image zooming to its position in
the crop controller while the background fades in.
@param viewController The parent controller that this view controller would be presenting from.
@param fromView A view that's frame will be used as the origin for this animation. Optional if `fromFrame` has a value.
@param fromFrame In the screen's coordinate space, the frame from which the image should animate from. Optional if `fromView` has a value.
@param setup A block that is called just before the transition starts. Recommended for hiding any necessary image views.
@param completion A block that is called once the transition animation is completed.
*/
public func presentAnimatedFrom(_ viewController: UIViewController, fromView view: UIView?, fromFrame frame: CGRect,
setup: (() -> (Void))?, completion: (() -> (Void))?)
{
toCropViewController.presentAnimatedFrom(viewController, view: view, frame: frame, setup: setup, completion: completion)
}
/**
Play a custom animation of the target image zooming to its position in
the crop controller while the background fades in. Additionally, if you're
'restoring' to a previous crop setup, this method lets you provide a previously
cropped copy of the image, and the previous crop settings to transition back to
where the user would have left off.
@param viewController The parent controller that this view controller would be presenting from.
@param image The previously cropped image that can be used in the transition animation.
@param fromView A view that's frame will be used as the origin for this animation. Optional if `fromFrame` has a value.
@param fromFrame In the screen's coordinate space, the frame from which the image should animate from.
@param angle The rotation angle in which the image was rotated when it was originally cropped.
@param toFrame In the image's coordinate space, the previous crop frame that created the previous crop
@param setup A block that is called just before the transition starts. Recommended for hiding any necessary image views.
@param completion A block that is called once the transition animation is completed.
*/
public func presentAnimatedFrom(_ viewController: UIViewController, fromImage image: UIImage?,
fromView: UIView?, fromFrame: CGRect, angle: Int, toImageFrame toFrame: CGRect,
setup: (() -> (Void))?, completion:(() -> (Void))?)
{
toCropViewController.presentAnimatedFrom(viewController, fromImage: image, fromView: fromView,
fromFrame: fromFrame, angle: angle, toFrame: toFrame,
setup: setup, completion: completion)
}
/**
Play a custom animation of the supplied cropped image zooming out from
the cropped frame to the specified frame as the rest of the content fades out.
If any view configurations need to be done before the animation starts,
@param viewController The parent controller that this view controller would be presenting from.
@param toView A view who's frame will be used to establish the destination frame
@param frame The target frame that the image will animate to
@param setup A block that is called just before the transition starts. Recommended for hiding any necessary image views.
@param completion A block that is called once the transition animation is completed.
*/
public func dismissAnimatedFrom(_ viewController: UIViewController, toView: UIView?, toFrame: CGRect,
setup: (() -> (Void))?, completion:(() -> (Void))?)
{
toCropViewController.dismissAnimatedFrom(viewController, toView: toView, toFrame: toFrame, setup: setup, completion: completion)
}
/**
Play a custom animation of the supplied cropped image zooming out from
the cropped frame to the specified frame as the rest of the content fades out.
If any view configurations need to be done before the animation starts,
@param viewController The parent controller that this view controller would be presenting from.
@param image The resulting 'cropped' image. If supplied, will animate out of the crop box zone. If nil, the default image will entirely zoom out
@param toView A view who's frame will be used to establish the destination frame
@param frame The target frame that the image will animate to
@param setup A block that is called just before the transition starts. Recommended for hiding any necessary image views.
@param completion A block that is called once the transition animation is completed.
*/
public func dismissAnimatedFrom(_ viewController: UIViewController, withCroppedImage croppedImage: UIImage?, toView: UIView?,
toFrame: CGRect, setup: (() -> (Void))?, completion:(() -> (Void))?)
{
toCropViewController.dismissAnimatedFrom(viewController, croppedImage: croppedImage, toView: toView,
toFrame: toFrame, setup: setup, completion: completion)
}
}
extension CropViewController {
fileprivate func setUpCropController() {
modalPresentationStyle = .fullScreen
addChild(toCropViewController)
transitioningDelegate = (toCropViewController as! (any UIViewControllerTransitioningDelegate))
toCropViewController.delegate = self
toCropViewController.didMove(toParent: self)
}
fileprivate func setUpDelegateHandlers() {
guard let delegate = self.delegate else {
onDidTapDone = nil
onDidCropToRect = nil
onDidCropImageToRect = nil
onDidCropToCircleImage = nil
onDidFinishCancelled = nil
return
}
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewControllerDidTapDone(_:))) {
self.onDidTapDone = { [weak self] in
guard let self else { return }
delegate.cropViewControllerDidTapDone?(self)
}
}
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewController(_:didCropImageToRect:angle:))) {
self.onDidCropImageToRect = {[weak self] rect, angle in
guard let strongSelf = self else { return }
delegate.cropViewController!(strongSelf, didCropImageToRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewController(_:didCropToImage:withRect:angle:))) {
self.onDidCropToRect = {[weak self] image, rect, angle in
guard let strongSelf = self else { return }
delegate.cropViewController!(strongSelf, didCropToImage: image, withRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewController(_:didCropToCircularImage:withRect:angle:))) {
self.onDidCropToCircleImage = {[weak self] image, rect, angle in
guard let strongSelf = self else { return }
delegate.cropViewController!(strongSelf, didCropToCircularImage: image, withRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector((any CropViewControllerDelegate).cropViewController(_:didFinishCancelled:))) {
self.onDidFinishCancelled = {[weak self] finished in
guard let strongSelf = self else { return }
delegate.cropViewController!(strongSelf, didFinishCancelled: finished)
}
}
}
}