Skip to content

Commit

Permalink
feat: Output realtime transformation for a special use case (#363)
Browse files Browse the repository at this point in the history
* feat: Output realtime transformation for a special use case

Add cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation)
Fix #362

* chore: Update an available message

* chore: Rename manualZoomed to isManuallyZoomed
  • Loading branch information
guoyingtao committed Dec 5, 2023
1 parent 1ed7f6c commit a5f9728
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 15 deletions.
12 changes: 7 additions & 5 deletions Example/ViewController.swift
Expand Up @@ -9,7 +9,7 @@
import UIKit
import Mantis

class ViewController: UIViewController, CropViewControllerDelegate {
class ViewController: UIViewController {
var image = UIImage(named: "sunflower.jpg")

@IBOutlet weak var croppedImageView: UIImageView!
Expand Down Expand Up @@ -74,7 +74,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
let transform = Transformation(offset: CGPoint(x: 169, y: 152),
rotation: -0.46043267846107483,
scale: 2.129973210831677,
manualZoomed: true,
isManuallyZoomed: true,
initialMaskFrame: CGRect(x: 14.0, y: 33, width: 402, height: 603),
maskFrame: CGRect(x: 67.90047201716507, y: 14.0, width: 294.19905596566986, height: 641.0),
cropWorkbenchViewBounds: CGRect(x: 169,
Expand Down Expand Up @@ -300,13 +300,15 @@ class ViewController: UIViewController, CropViewControllerDelegate {
croppedImageView.image = cropped
dismiss(animated: true)
}

}

extension ViewController: CropViewControllerDelegate {
func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage) {
dismiss(animated: true)
}

func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController) {
print("image is transformed.")
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation) {
print("image is transformed. transformation is \(transformation)")
}

func cropViewController(_ cropViewController: CropViewController, didBecomeResettable resettable: Bool) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropData.swift
Expand Up @@ -28,7 +28,7 @@ public typealias Transformation = (
offset: CGPoint,
rotation: CGFloat,
scale: CGFloat,
manualZoomed: Bool,
isManuallyZoomed: Bool,
initialMaskFrame: CGRect,
maskFrame: CGRect,
cropWorkbenchViewBounds: CGRect,
Expand Down
Expand Up @@ -31,7 +31,7 @@ extension CropView: UIScrollViewDelegate {
delegate?.cropViewDidEndResize(self)
makeSureImageContainsCropOverlay()

manualZoomed = true
isManuallyZoomed = true
viewModel.setBetweenOperationStatus()
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/Mantis/CropView/CropView.swift
Expand Up @@ -58,7 +58,7 @@ final class CropView: UIView {
}
}

var manualZoomed = false
var isManuallyZoomed = false
var forceFixedRatio = false
var checkForForceFixedRatioFlag = false
let cropViewConfig: CropViewConfig
Expand Down Expand Up @@ -587,7 +587,7 @@ extension CropView {
completion()
}

manualZoomed = true
isManuallyZoomed = true
}

func makeSureImageContainsCropOverlay() {
Expand All @@ -602,9 +602,9 @@ extension CropView {

cropWorkbenchView.updateLayout(byNewSize: CGSize(width: width, height: height))

if !manualZoomed || cropWorkbenchView.shouldScale() {
if !isManuallyZoomed || cropWorkbenchView.shouldScale() {
cropWorkbenchView.zoomScaleToBound(animated: false)
manualZoomed = false
isManuallyZoomed = false
} else {
cropWorkbenchView.updateMinZoomScale()
}
Expand Down Expand Up @@ -660,7 +660,7 @@ extension CropView {
offset: cropWorkbenchView.contentOffset,
rotation: getTotalRadians(),
scale: cropWorkbenchView.zoomScale,
manualZoomed: manualZoomed,
isManuallyZoomed: isManuallyZoomed,
initialMaskFrame: getInitialCropBoxRect(),
maskFrame: cropAuxiliaryIndicatorView.frame,
cropWorkbenchViewBounds: cropWorkbenchView.bounds,
Expand Down Expand Up @@ -963,7 +963,7 @@ extension CropView: CropViewProtocol {
cropWorkbenchView.bounds = transformation.cropWorkbenchViewBounds
}

manualZoomed = transformation.manualZoomed
isManuallyZoomed = transformation.isManuallyZoomed
cropWorkbenchView.zoomScale = transformation.scale
cropWorkbenchView.contentOffset = transformation.offset
viewModel.setBetweenOperationStatus()
Expand Down Expand Up @@ -1034,11 +1034,11 @@ extension CropView: CropViewProtocol {
maskFrame.origin.x += (cropFrame.width - maskFrame.width) / 2
}

let manualZoomed = (scale != 1.0)
let isManuallyZoomed = (scale != 1.0)
let transformation = Transformation(offset: offset,
rotation: 0,
scale: scale,
manualZoomed: manualZoomed,
isManuallyZoomed: isManuallyZoomed,
initialMaskFrame: .zero,
maskFrame: maskFrame,
cropWorkbenchViewBounds: .zero,
Expand Down
1 change: 1 addition & 0 deletions Sources/Mantis/CropViewController/CropViewController.swift
Expand Up @@ -377,6 +377,7 @@ extension CropViewController: CropViewDelegate {
func cropViewDidBecomeResettable(_ cropView: CropViewProtocol) {
cropToolbar.handleCropViewDidBecomeResettable()
delegate?.cropViewControllerDidImageTransformed(self)
delegate?.cropViewControllerDidImageTransformed(self, transformation: cropView.makeTransformation())
delegate?.cropViewController(self, didBecomeResettable: true)
}

Expand Down
6 changes: 6 additions & 0 deletions Sources/Mantis/Protocols/CropViewControllerDelegate.swift
Expand Up @@ -17,7 +17,12 @@ public protocol CropViewControllerDelegate: AnyObject {

func cropViewControllerDidBeginResize(_ cropViewController: CropViewController)
func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo)

@available(*, deprecated, message: "Use cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation) instead")
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController)

func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation)

func cropViewController(_ cropViewController: CropViewController, didBecomeResettable resettable: Bool)
}

Expand All @@ -26,5 +31,6 @@ public extension CropViewControllerDelegate {
func cropViewControllerDidBeginResize(_ cropViewController: CropViewController) {}
func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {}
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController) {}
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController, transformation: Transformation) {}
func cropViewController(_ cropViewController: CropViewController, didBecomeResettable resettable: Bool) {}
}
1 change: 1 addition & 0 deletions Sources/Mantis/Protocols/CropViewProtocol.swift
Expand Up @@ -47,6 +47,7 @@ protocol CropViewProtocol: UIView {
func getExpectedCropImageSize() -> CGSize

func rotate(by angle: Angle)
func makeTransformation() -> Transformation
}

extension CropViewProtocol {
Expand Down
4 changes: 4 additions & 0 deletions Tests/MantisTests/Mock/FakeCropView.swift
Expand Up @@ -86,6 +86,10 @@ class FakeCropView: UIView, CropViewProtocol {

}

func makeTransformation() -> Mantis.Transformation {
return Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false)
}

func crop() -> CropOutput {
CropOutput(nil,
Transformation(.zero, .zero, .zero, false, .zero, .zero, .zero, false, false),
Expand Down

0 comments on commit a5f9728

Please sign in to comment.