Skip to content

Commit

Permalink
feat: expose CropInfo in cropViewControllerDidCrop (#157)
Browse files Browse the repository at this point in the history
* feat: expose CropInfo in cropViewControllerDidCrop
* chore: update readme for cropViewControllerDidCrop
  • Loading branch information
guoyingtao committed Jan 14, 2022
1 parent 4c5de48 commit a0e3f39
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
7 changes: 5 additions & 2 deletions Example/EmbeddedCropViewController.swift
Expand Up @@ -59,7 +59,10 @@ class EmbeddedCropViewController: UIViewController {
}

extension EmbeddedCropViewController: CropViewControllerDelegate {
func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation) {
func cropViewControllerDidCrop(_ cropViewController: CropViewController,
cropped: UIImage,
transformation: Transformation,
cropInfo: CropInfo) {
self.dismiss(animated: true)
self.didGetCroppedImage?(cropped)
}
Expand All @@ -73,7 +76,7 @@ extension EmbeddedCropViewController: CropViewControllerDelegate {
}

func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {
let croppedImage = Mantis.getCroppedImage(byCropInfo: cropInfo, andImage: original)
let croppedImage = Mantis.crop(image: original, by: cropInfo)
self.resolutionLabel.text = getResolution(image: croppedImage)
}

Expand Down
11 changes: 9 additions & 2 deletions Example/ViewController.swift
Expand Up @@ -20,6 +20,9 @@ class ViewController: UIViewController, CropViewControllerDelegate {
super.viewDidLoad()

self.imagePicker = ImagePicker(presentationController: self, delegate: self)

// let cropInfo = CropInfo(translation: CGPoint(x: 94.13632915167773, y: 249.0606067401352), rotation: 0.7700574398040771, scale: 3.7635509462072614, cropSize: CGSize(width: 230.31290201917224, height: 728.0), imageViewSize: CGSize(width: 400.0, height: 600.0))
// croppedImageView.image = Mantis.crop(image: image!, by: cropInfo)
}

override var preferredStatusBarStyle: UIStatusBarStyle {
Expand Down Expand Up @@ -247,8 +250,12 @@ class ViewController: UIViewController, CropViewControllerDelegate {
}
}

func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation) {
print(transformation);
func cropViewControllerDidCrop(_ cropViewController: CropViewController,
cropped: UIImage,
transformation: Transformation,
cropInfo: CropInfo) {
print("transformation is \(transformation)");
print("cropInfo is \(cropInfo)");
croppedImageView.image = cropped
self.dismiss(animated: true)
}
Expand Down
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -70,7 +70,7 @@ github "guoyingtao/Mantis"
* The caller needs to conform CropViewControllerDelegate
```swift
public protocol CropViewControllerDelegate: class {
func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation)
func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, cropInfo: CropInfo)
func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage)

// The implementaion of the following functions are optional
Expand Down Expand Up @@ -182,7 +182,7 @@ public enum PresetTransformationType {
case presetNormalizedInfo(normailizedInfo: CGRect)
}
```
Please use the transformation infomation obtained previously from delegate method cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation).
Please use the transformation infomation obtained previously from delegate method cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, , cropInfo: CropInfo).

</details>
Expand Down
26 changes: 13 additions & 13 deletions Sources/Mantis/CropView/CropView.swift
Expand Up @@ -552,9 +552,9 @@ extension CropView {

// MARK: - internal API
extension CropView {
func crop(_ image: UIImage) -> (croppedImage: UIImage?, transformation: Transformation) {
func crop(_ image: UIImage) -> (croppedImage: UIImage?, transformation: Transformation, cropInfo: CropInfo) {

let info = getCropInfo()
let cropInfo = getCropInfo()

let transformation = Transformation(
offset: scrollView.contentOffset,
Expand All @@ -566,8 +566,8 @@ extension CropView {
scrollBounds: scrollView.bounds
)

guard let croppedImage = image.getCroppedImage(byCropInfo: info) else {
return (nil, transformation)
guard let croppedImage = image.crop(by: cropInfo) else {
return (nil, transformation, cropInfo)
}

switch cropShapeType {
Expand All @@ -579,24 +579,24 @@ extension CropView {
.diamond(maskOnly: true),
.heart(maskOnly: true),
.polygon(_, _, maskOnly: true):
return (croppedImage, transformation)
return (croppedImage, transformation, cropInfo)
case .ellipse:
return (croppedImage.ellipseMasked, transformation)
return (croppedImage.ellipseMasked, transformation, cropInfo)
case .circle:
return (croppedImage.ellipseMasked, transformation)
return (croppedImage.ellipseMasked, transformation, cropInfo)
case .roundedRect(let radiusToShortSide, maskOnly: false):
let radius = min(croppedImage.size.width, croppedImage.size.height) * radiusToShortSide
return (croppedImage.roundRect(radius), transformation)
return (croppedImage.roundRect(radius), transformation, cropInfo)
case .path(let points, maskOnly: false):
return (croppedImage.clipPath(points), transformation)
return (croppedImage.clipPath(points), transformation, cropInfo)
case .diamond(maskOnly: false):
let points = [CGPoint(x: 0.5, y: 0), CGPoint(x: 1, y: 0.5), CGPoint(x: 0.5, y: 1), CGPoint(x: 0, y: 0.5)]
return (croppedImage.clipPath(points), transformation)
return (croppedImage.clipPath(points), transformation, cropInfo)
case .heart(maskOnly: false):
return (croppedImage.heart, transformation)
return (croppedImage.heart, transformation, cropInfo)
case .polygon(let sides, let offset, maskOnly: false):
let points = polygonPointArray(sides: sides, originX: 0.5, originY: 0.5, radius: 0.5, offset: 90 + offset)
return (croppedImage.clipPath(points), transformation)
return (croppedImage.clipPath(points), transformation, cropInfo)
}
}

Expand All @@ -623,7 +623,7 @@ extension CropView {
return forceFixedRatio ? viewModel.radians : viewModel.getTotalRadians()
}

func crop() -> (croppedImage: UIImage?, transformation: Transformation) {
func crop() -> (croppedImage: UIImage?, transformation: Transformation, cropInfo: CropInfo) {
return crop(image)
}

Expand Down
14 changes: 11 additions & 3 deletions Sources/Mantis/CropViewController/CropViewController.swift
Expand Up @@ -26,7 +26,9 @@ import UIKit

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

Expand Down Expand Up @@ -439,7 +441,10 @@ public class CropViewController: UIViewController {
return
}

self.delegate?.cropViewControllerDidCrop(self, cropped: image, transformation: cropResult.transformation)
self.delegate?.cropViewControllerDidCrop(self,
cropped: image,
transformation: cropResult.transformation,
cropInfo: cropResult.cropInfo)
}
}

Expand Down Expand Up @@ -558,7 +563,10 @@ extension CropViewController {
return
}

delegate?.cropViewControllerDidCrop(self, cropped: image, transformation: cropResult.transformation)
delegate?.cropViewControllerDidCrop(self,
cropped: image,
transformation: cropResult.transformation,
cropInfo: cropResult.cropInfo)
}

public func process(_ image: UIImage) -> UIImage? {
Expand Down
14 changes: 7 additions & 7 deletions Sources/Mantis/Extensions/UIImageExtensions.swift
Expand Up @@ -109,21 +109,21 @@ extension UIImage {
}
}

func getCroppedImage(byCropInfo info: CropInfo) -> UIImage? {
func crop(by cropInfo: CropInfo) -> UIImage? {
guard let fixedImage = self.cgImageWithFixedOrientation() else {
return nil
}

var transform = CGAffineTransform.identity
transform = transform.translatedBy(x: info.translation.x, y: info.translation.y)
transform = transform.rotated(by: info.rotation)
transform = transform.scaledBy(x: info.scale, y: info.scale)
transform = transform.translatedBy(x: cropInfo.translation.x, y: cropInfo.translation.y)
transform = transform.rotated(by: cropInfo.rotation)
transform = transform.scaledBy(x: cropInfo.scale, y: cropInfo.scale)

guard let imageRef = fixedImage.transformedImage(transform,
zoomScale: info.scale,
zoomScale: cropInfo.scale,
sourceSize: self.size,
cropSize: info.cropSize,
imageViewSize: info.imageViewSize) else {
cropSize: cropInfo.cropSize,
imageViewSize: cropInfo.imageViewSize) else {
return nil
}

Expand Down
11 changes: 8 additions & 3 deletions Sources/Mantis/Mantis.swift
Expand Up @@ -30,7 +30,7 @@ private(set) var bundle: Bundle? = {

internal var localizationConfig = LocalizationConfig()

// MARK: - Functions
// MARK: - APIs
public func cropViewController(image: UIImage,
config: Mantis.Config = Mantis.Config(),
cropToolbar: CropToolbarProtocol = CropToolbar(frame: CGRect.zero)) -> CropViewController {
Expand All @@ -53,8 +53,13 @@ public func locateResourceBundle(by hostClass: AnyClass) {
LocalizedHelper.setBundle(Bundle(for: hostClass))
}

public func getCroppedImage(byCropInfo info: CropInfo, andImage image: UIImage) -> UIImage? {
return image.getCroppedImage(byCropInfo: info)
@available(*, deprecated, renamed: "crop(image:by:)")
public func getCroppedImage(byCropInfo cropInfo: CropInfo, andImage image: UIImage) -> UIImage? {
return image.crop(by: cropInfo)
}

public func crop(image: UIImage, by cropInfo: CropInfo) -> UIImage? {
return image.crop(by: cropInfo)
}

// MARK: - Type Aliases
Expand Down

0 comments on commit a0e3f39

Please sign in to comment.