Skip to content

Commit

Permalink
feat: add CropRegion into CropInfo
Browse files Browse the repository at this point in the history
add getCropRegion to ImageContainerProtocol
  • Loading branch information
Yingtao Guo committed Jan 20, 2023
1 parent ccd462d commit 035b079
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 23 deletions.
4 changes: 4 additions & 0 deletions Mantis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
66F4398A2978539700728B52 /* RotationDialViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F439892978539700728B52 /* RotationDialViewModelTests.swift */; };
66F4398E2978F6E400728B52 /* UIViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F4398D2978F6E400728B52 /* UIViewExtensions.swift */; };
66F439902979472C00728B52 /* CropViewModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F4398F2979472C00728B52 /* CropViewModelTests.swift */; };
66F43992297A123400728B52 /* CropViewControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F43991297A123400728B52 /* CropViewControllerDelegate.swift */; };
66F909BF287D22E7007E2FC6 /* ToolBarButtonImageBuilder+DrawImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 66F909BE287D22E7007E2FC6 /* ToolBarButtonImageBuilder+DrawImage.swift */; };
8EFB6F6725D16B0F00C0DDB2 /* MantisLocalizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8EFB6F3525D154D900C0DDB2 /* MantisLocalizable.strings */; };
8EFB6FB125D187A000C0DDB2 /* Resource.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 8EFB6F5425D16A9E00C0DDB2 /* Resource.bundle */; };
Expand Down Expand Up @@ -133,6 +134,7 @@
66F439892978539700728B52 /* RotationDialViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RotationDialViewModelTests.swift; sourceTree = "<group>"; };
66F4398D2978F6E400728B52 /* UIViewExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewExtensions.swift; sourceTree = "<group>"; };
66F4398F2979472C00728B52 /* CropViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CropViewModelTests.swift; sourceTree = "<group>"; };
66F43991297A123400728B52 /* CropViewControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CropViewControllerDelegate.swift; sourceTree = "<group>"; };
66F909BE287D22E7007E2FC6 /* ToolBarButtonImageBuilder+DrawImage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ToolBarButtonImageBuilder+DrawImage.swift"; sourceTree = "<group>"; };
8EFB6F3625D154D900C0DDB2 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/MantisLocalizable.strings; sourceTree = "<group>"; };
8EFB6F3725D154D900C0DDB2 /* ar */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ar; path = ar.lproj/MantisLocalizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -236,6 +238,7 @@
66100E08294BAE46001C8593 /* ImageContainerProtocol.swift */,
66100E0E294BAEA5001C8593 /* RotationDialProtocol.swift */,
66F439872978376F00728B52 /* RotationDialViewModelProtocol.swift */,
66F43991297A123400728B52 /* CropViewControllerDelegate.swift */,
);
path = Protocols;
sourceTree = "<group>";
Expand Down Expand Up @@ -649,6 +652,7 @@
OBJ_97 /* RotationCalculator.swift in Sources */,
OBJ_98 /* RotationDial+Touches.swift in Sources */,
66100E0B294BAE83001C8593 /* CropViewModelProtocol.swift in Sources */,
66F43992297A123400728B52 /* CropViewControllerDelegate.swift in Sources */,
OBJ_99 /* RotationDial.swift in Sources */,
6659429D2877DAE80096A5DB /* Config.swift in Sources */,
OBJ_100 /* RotationDialPlate.swift in Sources */,
Expand Down
10 changes: 8 additions & 2 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1052,13 +1052,19 @@ extension CropView: CropViewProtocol {
}
}

let totalRadians = getTotalRadians()
let cropRegion = imageContainer.getCropRegion(withCropBoxFrame: viewModel.cropBoxFrame,
cropView: self,
rotation: totalRadians)

return CropInfo(
translation: translation,
rotation: getTotalRadians(),
rotation: totalRadians,
scaleX: scaleX,
scaleY: scaleY,
cropSize: cropAuxiliaryIndicatorView.frame.size,
imageViewSize: imageContainer.bounds.size
imageViewSize: imageContainer.bounds.size,
cropRegion: cropRegion
)
}

Expand Down
18 changes: 18 additions & 0 deletions Sources/Mantis/CropView/ImageContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ extension ImageContainer: ImageContainerProtocol {

return refBounds.contains(point1) && refBounds.contains(point2)
}

func getCropRegion(withCropBoxFrame cropBoxFrame: CGRect, cropView: UIView, rotation: CGFloat) -> CropRegion {
var topLeft = cropView.convert(CGPoint(x: cropBoxFrame.minX, y: cropBoxFrame.minY), to: self)
var topRight = cropView.convert(CGPoint(x: cropBoxFrame.maxX, y: cropBoxFrame.minY), to: self)
var bottomLeft = cropView.convert(CGPoint(x: cropBoxFrame.minX, y: cropBoxFrame.maxY), to: self)
var bottomRight = cropView.convert(CGPoint(x: cropBoxFrame.maxX, y: cropBoxFrame.maxY), to: self)

topLeft = CGPoint(x: topLeft.x / bounds.width, y: topLeft.y / bounds.height)
topRight = CGPoint(x: topRight.x / bounds.width, y: topRight.y / bounds.height)
bottomLeft = CGPoint(x: bottomLeft.x / bounds.width, y: bottomLeft.y / bounds.height)
bottomRight = CGPoint(x: bottomRight.x / bounds.width, y: bottomRight.y / bounds.height)

return CropRegion(topLeft: topLeft,
topRight: topRight,
bottomLeft: bottomLeft,
bottomRight: bottomRight,
rotation: rotation)
}
}
20 changes: 0 additions & 20 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,6 @@

import UIKit

public protocol CropViewControllerDelegate: AnyObject {
func cropViewControllerDidCrop(_ cropViewController: CropViewController,
cropped: UIImage,
transformation: Transformation,
cropInfo: CropInfo)
func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage)
func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage)

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

public extension CropViewControllerDelegate where Self: UIViewController {
func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage) {}
func cropViewControllerDidBeginResize(_ cropViewController: CropViewController) {}
func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {}
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController) {}
}

public class CropViewController: UIViewController {
public weak var delegate: CropViewControllerDelegate?
public var config = Mantis.Config()
Expand Down
28 changes: 28 additions & 0 deletions Sources/Mantis/Protocols/CropViewControllerDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// CropViewControllerDelegate.swift
// Mantis
//
// Created by yingtguo on 1/20/23.
//

import UIKit

public protocol CropViewControllerDelegate: AnyObject {
func cropViewControllerDidCrop(_ cropViewController: CropViewController,
cropped: UIImage,
transformation: Transformation,
cropInfo: CropInfo)
func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage)
func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage)

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

public extension CropViewControllerDelegate where Self: UIViewController {
func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage) {}
func cropViewControllerDidBeginResize(_ cropViewController: CropViewController) {}
func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {}
func cropViewControllerDidImageTransformed(_ cropViewController: CropViewController) {}
}
1 change: 1 addition & 0 deletions Sources/Mantis/Protocols/ImageContainerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import UIKit

protocol ImageContainerProtocol: UIView {
func contains(rect: CGRect, fromView view: UIView, tolerance: CGFloat) -> Bool
func getCropRegion(withCropBoxFrame cropBoxFrame: CGRect, cropView: UIView, rotation: CGFloat) -> CropRegion
}
11 changes: 10 additions & 1 deletion Sources/Mantis/TypeAlias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ public typealias Transformation = (
cropWorkbenchViewBounds: CGRect
)

public typealias CropRegion = (
topLeft: CGPoint,
topRight: CGPoint,
bottomLeft: CGPoint,
bottomRight: CGPoint,
rotation: CGFloat
)

public typealias CropInfo = (
translation: CGPoint,
rotation: CGFloat,
scaleX: CGFloat,
scaleY: CGFloat,
cropSize: CGSize,
imageViewSize: CGSize
imageViewSize: CGSize,
cropRegion: CropRegion
)

typealias CropOutput = (
Expand Down

0 comments on commit 035b079

Please sign in to comment.