From a0e3f39c546a1852c32bb3677be562fc194748b8 Mon Sep 17 00:00:00 2001 From: Yingtao Guo Date: Fri, 14 Jan 2022 16:32:24 -0500 Subject: [PATCH] feat: expose CropInfo in cropViewControllerDidCrop (#157) * feat: expose CropInfo in cropViewControllerDidCrop * chore: update readme for cropViewControllerDidCrop --- Example/EmbeddedCropViewController.swift | 7 +++-- Example/ViewController.swift | 11 ++++++-- README.md | 4 +-- Sources/Mantis/CropView/CropView.swift | 26 +++++++++---------- .../CropViewController.swift | 14 +++++++--- .../Mantis/Extensions/UIImageExtensions.swift | 14 +++++----- Sources/Mantis/Mantis.swift | 11 +++++--- 7 files changed, 55 insertions(+), 32 deletions(-) diff --git a/Example/EmbeddedCropViewController.swift b/Example/EmbeddedCropViewController.swift index 729841f1..cdd9631f 100644 --- a/Example/EmbeddedCropViewController.swift +++ b/Example/EmbeddedCropViewController.swift @@ -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) } @@ -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) } diff --git a/Example/ViewController.swift b/Example/ViewController.swift index 74fa6292..9157572e 100644 --- a/Example/ViewController.swift +++ b/Example/ViewController.swift @@ -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 { @@ -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) } diff --git a/README.md b/README.md index dca016bc..20115fef 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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). diff --git a/Sources/Mantis/CropView/CropView.swift b/Sources/Mantis/CropView/CropView.swift index 6429d53b..6b9efbfc 100644 --- a/Sources/Mantis/CropView/CropView.swift +++ b/Sources/Mantis/CropView/CropView.swift @@ -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, @@ -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 { @@ -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) } } @@ -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) } diff --git a/Sources/Mantis/CropViewController/CropViewController.swift b/Sources/Mantis/CropViewController/CropViewController.swift index f074cab2..18b69026 100644 --- a/Sources/Mantis/CropViewController/CropViewController.swift +++ b/Sources/Mantis/CropViewController/CropViewController.swift @@ -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) @@ -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) } } @@ -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? { diff --git a/Sources/Mantis/Extensions/UIImageExtensions.swift b/Sources/Mantis/Extensions/UIImageExtensions.swift index 86d6d20c..d0095a7a 100644 --- a/Sources/Mantis/Extensions/UIImageExtensions.swift +++ b/Sources/Mantis/Extensions/UIImageExtensions.swift @@ -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 } diff --git a/Sources/Mantis/Mantis.swift b/Sources/Mantis/Mantis.swift index 3e0ff748..056a493f 100644 --- a/Sources/Mantis/Mantis.swift +++ b/Sources/Mantis/Mantis.swift @@ -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 { @@ -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