Skip to content

Commit

Permalink
fix: fix fix-ratio not changing when switch to free ratio, change cro…
Browse files Browse the repository at this point in the history
…p box then switch back (#351)

* fix: fix fix-ratio not changing when switch to free ratio, change crop box then switch back

fix some unit test compile errors

* refactor: rename aspectRatio to fixedImageRatio
  • Loading branch information
guoyingtao committed Nov 2, 2023
1 parent 68e3b43 commit 744cc2a
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
width: 548.380489739444,
height: 704.9696330065433),
horizontallyFlipped: true,
verticallyFlipped:false)
verticallyFlipped: false)
config.cropToolbarConfig.toolbarButtonOptions = [.clockwiseRotate, .reset, .ratio, .autoAdjust, .horizontallyFlip]
config.cropViewConfig.presetTransformationType = .presetInfo(info: transform)
config.cropViewConfig.builtInRotationControlViewType = .slideDial()
Expand Down
15 changes: 10 additions & 5 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -863,8 +863,8 @@ extension CropView: CropViewProtocol {
func setFixedRatio(_ ratio: Double, zoom: Bool = true, presetFixedRatioType: PresetFixedRatioType) {
aspectRatioLockEnabled = true

if viewModel.aspectRatio != CGFloat(ratio) {
viewModel.aspectRatio = CGFloat(ratio)
if viewModel.fixedImageRatio != CGFloat(ratio) {
viewModel.fixedImageRatio = CGFloat(ratio)

setForceFixedRatio(by: presetFixedRatioType)

Expand Down Expand Up @@ -920,7 +920,7 @@ extension CropView: CropViewProtocol {
func handleAlterCropper90Degree() {
let ratio = Double(cropAuxiliaryIndicatorView.frame.height / cropAuxiliaryIndicatorView.frame.width)

viewModel.aspectRatio = CGFloat(ratio)
viewModel.fixedImageRatio = CGFloat(ratio)

UIView.animate(withDuration: 0.5) {
self.setFixedRatioCropBox()
Expand All @@ -931,13 +931,18 @@ extension CropView: CropViewProtocol {
aspectRatioLockEnabled = true

if ratio == 0 {
viewModel.aspectRatio = transformation.maskFrame.width / transformation.maskFrame.height
viewModel.fixedImageRatio = transformation.maskFrame.width / transformation.maskFrame.height
} else {
viewModel.aspectRatio = CGFloat(ratio)
viewModel.fixedImageRatio = CGFloat(ratio)
setFixedRatioCropBox(zoom: false, cropBox: viewModel.cropBoxFrame)
}
}

func setFreeCrop() {
aspectRatioLockEnabled = false
viewModel.fixedImageRatio = -1
}

func transform(byTransformInfo transformation: Transformation, isUpdateRotationControlView: Bool = true) {
viewModel.setRotatingStatus(by: Angle(radians: transformation.rotation))

Expand Down
10 changes: 5 additions & 5 deletions Sources/Mantis/CropView/CropViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ final class CropViewModel: CropViewModelProtocol {
}

var rotationType: ImageRotationType = .none
var aspectRatio: CGFloat = -1
var fixedImageRatio: CGFloat = -1
var cropLeftTopOnImage = CGPoint.zero
var cropRightBottomOnImage = CGPoint(x: 1, y: 1)

Expand All @@ -95,7 +95,7 @@ final class CropViewModel: CropViewModelProtocol {
rotationType = .none

if forceFixedRatio == false {
aspectRatio = -1
fixedImageRatio = -1
}

cropLeftTopOnImage = .zero
Expand Down Expand Up @@ -185,10 +185,10 @@ final class CropViewModel: CropViewModelProtocol {
var cropBoxFrame = refCropBox
let center = cropBoxFrame.center

if aspectRatio > CGFloat(imageHorizontalToVerticalRatio.ratio) {
cropBoxFrame.size.height = cropBoxFrame.width / aspectRatio
if fixedImageRatio > CGFloat(imageHorizontalToVerticalRatio.ratio) {
cropBoxFrame.size.height = cropBoxFrame.width / fixedImageRatio
} else {
cropBoxFrame.size.width = cropBoxFrame.height * aspectRatio
cropBoxFrame.size.width = cropBoxFrame.height * fixedImageRatio
}

cropBoxFrame.origin.x = center.x - cropBoxFrame.width / 2
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ open class CropViewController: UIViewController {
}

private func resetRatioButton() {
cropView.aspectRatioLockEnabled = false
cropView.setFreeCrop()
cropToolbar.handleFixedRatioUnSetted()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/Protocols/CropViewModelProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protocol CropViewModelProtocol: AnyObject {
var degrees: CGFloat { get set }
var radians: CGFloat { get }
var rotationType: ImageRotationType { get set }
var aspectRatio: CGFloat { get set }
var fixedImageRatio: CGFloat { get set }
var cropLeftTopOnImage: CGPoint { get set }
var cropRightBottomOnImage: CGPoint { get set }
var horizontallyFlip: Bool { get set }
Expand Down
1 change: 1 addition & 0 deletions Sources/Mantis/Protocols/CropViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protocol CropViewProtocol: UIView {
func getTransformInfo(byNormalizedInfo normalizedInfo: CGRect) -> Transformation
func processPresetTransformation(completion: (Transformation) -> Void)

func setFreeCrop()
func horizontallyFlip()
func verticallyFlip()
func reset()
Expand Down
16 changes: 8 additions & 8 deletions Tests/MantisTests/CropViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ final class CropViewModelTests: XCTestCase {
}

func testReset() {
viewModel.aspectRatio = 1
viewModel.fixedImageRatio = 1
viewModel.reset(forceFixedRatio: true)
assertResetStatus()
XCTAssertEqual(viewModel.aspectRatio, 1)
XCTAssertEqual(viewModel.fixedImageRatio, 1)

viewModel.aspectRatio = 1
viewModel.fixedImageRatio = 1
viewModel.reset(forceFixedRatio: false)
assertResetStatus()
XCTAssertEqual(viewModel.aspectRatio, -1)
XCTAssertEqual(viewModel.fixedImageRatio, -1)
}

private func assertResetStatus() {
Expand Down Expand Up @@ -187,18 +187,18 @@ final class CropViewModelTests: XCTestCase {

func testSetCropBoxFrame() {
let refCropBox = CGRect(x: 20, y: 20, width: 100, height: 200)
viewModel.aspectRatio = 0.9
viewModel.fixedImageRatio = 0.9
let imageHorizontalToVerticalRatio = ImageHorizontalToVerticalRatio(ratio: 1.2)
viewModel.setCropBoxFrame(by: refCropBox, for: imageHorizontalToVerticalRatio)
XCTAssertEqual(viewModel.cropBoxFrame.center, refCropBox.center)
XCTAssertEqual(viewModel.cropBoxFrame.height, refCropBox.height)
XCTAssertEqual(viewModel.cropBoxFrame.width / viewModel.cropBoxFrame.height, viewModel.aspectRatio)
XCTAssertEqual(viewModel.cropBoxFrame.width / viewModel.cropBoxFrame.height, viewModel.fixedImageRatio)

viewModel.aspectRatio = 1.4
viewModel.fixedImageRatio = 1.4
viewModel.setCropBoxFrame(by: refCropBox, for: imageHorizontalToVerticalRatio)
XCTAssertEqual(viewModel.cropBoxFrame.center, refCropBox.center)
XCTAssertEqual(viewModel.cropBoxFrame.width, refCropBox.width)
XCTAssertEqual(viewModel.cropBoxFrame.width / viewModel.cropBoxFrame.height, viewModel.aspectRatio)
XCTAssertEqual(viewModel.cropBoxFrame.width / viewModel.cropBoxFrame.height, viewModel.fixedImageRatio)
}

func testSetInitialStatus() {
Expand Down
12 changes: 8 additions & 4 deletions Tests/MantisTests/Mock/FakeCropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class FakeCropView: UIView, CropViewProtocol {

}

func setFreeCrop() {

}

func handlePresetFixedRatio(_ ratio: Double, transformation: Transformation) {

}
Expand All @@ -59,11 +63,11 @@ class FakeCropView: UIView, CropViewProtocol {
}

func getTransformInfo(byTransformInfo transformInfo: Transformation) -> Transformation {
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero)
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false)
}

func getTransformInfo(byNormalizedInfo normalizedInfo: CGRect) -> Transformation {
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero)
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false)
}

func processPresetTransformation(completion: (Transformation) -> Void) {
Expand All @@ -84,7 +88,7 @@ class FakeCropView: UIView, CropViewProtocol {

func crop() -> CropOutput {
CropOutput(nil,
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero),
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false),
CropInfo(.zero, .zero, .zero, .zero, .zero, .zero,
CropRegion(topLeft: .zero,
topRight: .zero,
Expand All @@ -94,7 +98,7 @@ class FakeCropView: UIView, CropViewProtocol {

func crop(_ image: UIImage) -> CropOutput {
CropOutput(nil,
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero),
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false),
CropInfo(.zero, .zero, .zero, .zero, .zero, .zero,
CropRegion(topLeft: .zero,
topRight: .zero,
Expand Down

0 comments on commit 744cc2a

Please sign in to comment.