From 4a3ead5c3c431aaae1e143d386fa5d6f99861d9d Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Thu, 15 Apr 2021 18:21:17 -0400 Subject: [PATCH 1/9] fix: solve the issue when using normalizedTransform with presetFixedRatioType --- Sources/Mantis/CropView/CropView.swift | 6 ++++-- Sources/Mantis/CropView/CropViewModel.swift | 4 ++-- .../CropViewController/CropViewController.swift | 15 +++++++++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index c2f3e78b..0641cc56 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -693,8 +693,10 @@ extension CropView { rotationDial?.rotateDialPlate(to: CGAngle(radians: radians), animated: false) } - func setFixedRatioCropBox() { - viewModel.setCropBoxFrame(by: getInitialCropBoxRect(), + func setFixedRatioCropBox(insideRect rect: CGRect? = nil) { + + let refRect = rect ?? getInitialCropBoxRect() + viewModel.setFixedRatioCropBoxFrame(by: refRect, and: getImageRatioH()) let contentRect = getContentBounds() diff --git a/Sources/Mantis/CropView/CropViewModel.swift b/Sources/Mantis/CropView/CropViewModel.swift index aecb14df..e83bb3e9 100644 --- a/Sources/Mantis/CropView/CropViewModel.swift +++ b/Sources/Mantis/CropView/CropViewModel.swift @@ -165,8 +165,8 @@ class CropViewModel: NSObject { return newCropBoxFrame } - func setCropBoxFrame(by initialCropBox: CGRect, and imageRationH: Double) { - var cropBoxFrame = initialCropBox + func setFixedRatioCropBoxFrame(by refCropBox: CGRect, and imageRationH: Double) { + var cropBoxFrame = refCropBox let center = cropBoxFrame.center if (aspectRatio > CGFloat(imageRationH)) { diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index cbe2a034..2b13d09d 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -109,7 +109,14 @@ public class CropViewController: UIViewController { switch(config.presetFixedRatioType) { case .alwaysUsingOnePresetFixedRatio(let ratio): config.cropToolbarConfig.includeFixedRatioSettingButton = false - setFixedRatio(ratio) + + if case .none = config.presetTransformationType { + setFixedRatio(ratio) + } else { + cropToolbar.handleFixedRatioSetted(ratio: ratio) + cropView.viewModel.aspectRatio = CGFloat(ratio) + } + case .canUseMultiplePresetFixedRatio(let defaultRatio): if (defaultRatio > 0) { setFixedRatio(defaultRatio) @@ -213,8 +220,7 @@ public class CropViewController: UIViewController { DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in self?.cropView.handleRotate() } - } - + } func setFixedRatio(_ ratio: Double) { cropToolbar.handleFixedRatioSetted(ratio: ratio) @@ -279,7 +285,8 @@ public class CropViewController: UIViewController { // Preset transformation changed preset fixed ratio crop box // So we need to reset it again. if case .alwaysUsingOnePresetFixedRatio = self.config.presetFixedRatioType { - self.cropView.setFixedRatioCropBox() + cropView.aspectRatioLockEnabled = true + self.cropView.setFixedRatioCropBox(insideRect: cropView.viewModel.cropBoxFrame) } } } From bb9bfd70cad5ec27b0545ddef007533bfb3f8678 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Mon, 19 Apr 2021 15:00:49 -0400 Subject: [PATCH 2/9] feat: new logic for alwaysUsingOnePresetFixedRatio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If we have preset transformation, we don’t need provide ratio to alwaysUsingOnePresetFixedRatio. Instead we directly use the ratio of preset transformation --- .../CropViewController/CropViewController.swift | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index 2b13d09d..ad53e849 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -113,8 +113,7 @@ public class CropViewController: UIViewController { if case .none = config.presetTransformationType { setFixedRatio(ratio) } else { - cropToolbar.handleFixedRatioSetted(ratio: ratio) - cropView.viewModel.aspectRatio = CGFloat(ratio) + cropView.aspectRatioLockEnabled = true } case .canUseMultiplePresetFixedRatio(let defaultRatio): @@ -278,17 +277,7 @@ public class CropViewController: UIViewController { public override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - - processPresetTransformation { [weak self] in - guard let self = self else { return } - - // Preset transformation changed preset fixed ratio crop box - // So we need to reset it again. - if case .alwaysUsingOnePresetFixedRatio = self.config.presetFixedRatioType { - cropView.aspectRatioLockEnabled = true - self.cropView.setFixedRatioCropBox(insideRect: cropView.viewModel.cropBoxFrame) - } - } + processPresetTransformation() } private func getTransformInfo(byTransformInfo transformInfo: Transformation) -> Transformation { From bab58bad54bd4323985e273450140054665e5050 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Mon, 19 Apr 2021 15:08:54 -0400 Subject: [PATCH 3/9] Fix a comile error --- Sources/Mantis/CropViewController/CropViewController.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index ad53e849..df1de346 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -255,7 +255,7 @@ public class CropViewController: UIViewController { } } - private func processPresetTransformation(completion: ()->Void) { + private func processPresetTransformation() { if case .presetInfo(let transformInfo) = config.presetTransformationType { var newTransform = getTransformInfo(byTransformInfo: transformInfo) @@ -266,12 +266,10 @@ public class CropViewController: UIViewController { let adjustScale = (cropView.viewModel.cropBoxFrame.width / cropView.viewModel.cropOrignFrame.width) / (transformInfo.maskFrame.width / transformInfo.intialMaskFrame.width) newTransform.scale *= adjustScale cropView.transform(byTransformInfo: newTransform) - completion() } else if case .presetNormalizedInfo(let normailizedInfo) = config.presetTransformationType { let transformInfo = getTransformInfo(byNormalizedInfo: normailizedInfo); cropView.transform(byTransformInfo: transformInfo) cropView.scrollView.frame = transformInfo.maskFrame - completion() } } From aba8568e9cadad3b3b3e9278b46ebc5553d5e919 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Mon, 19 Apr 2021 15:13:17 -0400 Subject: [PATCH 4/9] Add default ratio for alwaysUsingOnePresetFixedRatio --- Sources/Mantis/Mantis.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Mantis/Mantis.swift b/Sources/Mantis/Mantis.swift index 66b4639c..39fc72fc 100644 --- a/Sources/Mantis/Mantis.swift +++ b/Sources/Mantis/Mantis.swift @@ -71,7 +71,7 @@ public enum PresetTransformationType { public enum PresetFixedRatioType { /** When choose alwaysUsingOnePresetFixedRatio, fixed-ratio setting button does not show. */ - case alwaysUsingOnePresetFixedRatio(ratio: Double) + case alwaysUsingOnePresetFixedRatio(ratio: Double = 0) case canUseMultiplePresetFixedRatio(defaultRatio: Double = 0) } From c3d1b2a164ae6d500fef3cc2cafe1c62eccd6eae Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Mon, 19 Apr 2021 15:26:34 -0400 Subject: [PATCH 5/9] Set aspectRatioLockEnabled at right timings Set aspectRatioLockEnabled to true if config.presetFixedRatioType is alwaysUsingOnePresetFixedRatio for preset transformations --- .../CropViewController/CropViewController.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index df1de346..a198a600 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -112,8 +112,6 @@ public class CropViewController: UIViewController { if case .none = config.presetTransformationType { setFixedRatio(ratio) - } else { - cropView.aspectRatioLockEnabled = true } case .canUseMultiplePresetFixedRatio(let defaultRatio): @@ -255,7 +253,7 @@ public class CropViewController: UIViewController { } } - private func processPresetTransformation() { + private func processPresetTransformation(completion: ()->Void) { if case .presetInfo(let transformInfo) = config.presetTransformationType { var newTransform = getTransformInfo(byTransformInfo: transformInfo) @@ -266,16 +264,23 @@ public class CropViewController: UIViewController { let adjustScale = (cropView.viewModel.cropBoxFrame.width / cropView.viewModel.cropOrignFrame.width) / (transformInfo.maskFrame.width / transformInfo.intialMaskFrame.width) newTransform.scale *= adjustScale cropView.transform(byTransformInfo: newTransform) + completion() } else if case .presetNormalizedInfo(let normailizedInfo) = config.presetTransformationType { let transformInfo = getTransformInfo(byNormalizedInfo: normailizedInfo); cropView.transform(byTransformInfo: transformInfo) cropView.scrollView.frame = transformInfo.maskFrame + completion() } } public override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - processPresetTransformation() + processPresetTransformation() { [weak self] in + guard let self = self else { return } + if case .alwaysUsingOnePresetFixedRatio = self.config.presetFixedRatioType { + self.cropView.aspectRatioLockEnabled = true + } + } } private func getTransformInfo(byTransformInfo transformInfo: Transformation) -> Transformation { From 0d7b48a53beacc239aeb1924a91ec964cf4e006d Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Tue, 20 Apr 2021 15:27:46 -0400 Subject: [PATCH 6/9] Fix: set the correct value for intialMaskFrame of tranforamtion --- Sources/Mantis/CropView/CropView.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index 0641cc56..9afeee0c 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -549,7 +549,7 @@ extension CropView { rotation: getTotalRadians(), scale: scrollView.zoomScale, manualZoomed: manualZoomed, - intialMaskFrame: viewModel.cropOrignFrame, + intialMaskFrame: getInitialCropBoxRect(), maskFrame: gridOverlayView.frame, scrollBounds: scrollView.bounds ) From e1bb974da1b712aaeb46fb4f7e274289831823fd Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Thu, 22 Apr 2021 17:39:14 -0400 Subject: [PATCH 7/9] fix the manual crop offset issue after rotation --- Sources/Mantis/CropView/CropView.swift | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index 6913ec84..259af0ac 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -440,16 +440,20 @@ extension CropView { let radians = viewModel.getTotalRadians() // calculate the new bounds of scroll view - let width = abs(cos(radians)) * newCropBounds.size.width + abs(sin(radians)) * newCropBounds.size.height - let height = abs(sin(radians)) * newCropBounds.size.width + abs(cos(radians)) * newCropBounds.size.height + let newBoundWidth = abs(cos(radians)) * newCropBounds.size.width + abs(sin(radians)) * newCropBounds.size.height + let newBoundHeight = abs(sin(radians)) * newCropBounds.size.width + abs(cos(radians)) * newCropBounds.size.height // calculate the zoom area of scroll view var scaleFrame = viewModel.cropBoxFrame - if scaleFrame.width >= scrollView.contentSize.width { - scaleFrame.size.width = scrollView.contentSize.width + + let refContentWidth = abs(cos(radians)) * scrollView.contentSize.width + abs(sin(radians)) * scrollView.contentSize.height + let refContentHeight = abs(sin(radians)) * scrollView.contentSize.width + abs(cos(radians)) * scrollView.contentSize.height + + if scaleFrame.width >= refContentWidth { + scaleFrame.size.width = refContentWidth } - if scaleFrame.height >= scrollView.contentSize.height { - scaleFrame.size.height = scrollView.contentSize.height + if scaleFrame.height >= refContentHeight { + scaleFrame.size.height = refContentHeight } let contentOffset = scrollView.contentOffset @@ -457,10 +461,10 @@ extension CropView { y: (contentOffset.y + scrollView.bounds.height / 2)) - scrollView.bounds = CGRect(x: 0, y: 0, width: width, height: height) + scrollView.bounds = CGRect(x: 0, y: 0, width: newBoundWidth, height: newBoundHeight) - let newContentOffset = CGPoint(x: (contentOffsetCenter.x - scrollView.bounds.width / 2), - y: (contentOffsetCenter.y - scrollView.bounds.height / 2)) + let newContentOffset = CGPoint(x: (contentOffsetCenter.x - newBoundWidth / 2), + y: (contentOffsetCenter.y - newBoundHeight / 2)) scrollView.contentOffset = newContentOffset let newCropBoxFrame = GeometryHelper.getInscribeRect(fromOutsideRect: contentRect, andInsideRect: viewModel.cropBoxFrame) From 9e70b254825ca9dea604741367fdde0c59fb4575 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Thu, 22 Apr 2021 18:07:46 -0400 Subject: [PATCH 8/9] set fix ratio for preset transformation --- .../Mantis/CropViewController/CropViewController.swift | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index 6d8ec9f1..b9a12b57 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -253,7 +253,7 @@ public class CropViewController: UIViewController { } } - private func processPresetTransformation(completion: ()->Void) { + private func processPresetTransformation(completion: (Transformation)->Void) { if case .presetInfo(let transformInfo) = config.presetTransformationType { var newTransform = getTransformInfo(byTransformInfo: transformInfo) @@ -264,21 +264,22 @@ public class CropViewController: UIViewController { let adjustScale = (cropView.viewModel.cropBoxFrame.width / cropView.viewModel.cropOrignFrame.width) / (transformInfo.maskFrame.width / transformInfo.intialMaskFrame.width) newTransform.scale *= adjustScale cropView.transform(byTransformInfo: newTransform) - completion() + completion(transformInfo) } else if case .presetNormalizedInfo(let normailizedInfo) = config.presetTransformationType { let transformInfo = getTransformInfo(byNormalizedInfo: normailizedInfo); cropView.transform(byTransformInfo: transformInfo) cropView.scrollView.frame = transformInfo.maskFrame - completion() + completion(transformInfo) } } public override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) - processPresetTransformation() { [weak self] in + processPresetTransformation() { [weak self] transform in guard let self = self else { return } if case .alwaysUsingOnePresetFixedRatio = self.config.presetFixedRatioType { self.cropView.aspectRatioLockEnabled = true + self.cropView.viewModel.aspectRatio = transform.maskFrame.width / transform.maskFrame.height } } } From 78338cdbd7bf30077b8d88403baa40f7e02d00e3 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Thu, 22 Apr 2021 18:22:48 -0400 Subject: [PATCH 9/9] chore: improve format --- Sources/Mantis/CropView/CropView.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index 9dbe62d7..6b010ba6 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -702,8 +702,7 @@ extension CropView { func setFixedRatioCropBox(zoom: Bool = true) { - viewModel.setCropBoxFrame(by: getInitialCropBoxRect(), - and: getImageRatioH()) + viewModel.setCropBoxFrame(by: getInitialCropBoxRect(), and: getImageRatioH()) let contentRect = getContentBounds() adjustUIForNewCrop(contentRect: contentRect, animation: false, zoom: zoom) { [weak self] in