Skip to content

Commit

Permalink
feat: support updating image while cropping (#387)
Browse files Browse the repository at this point in the history
* feat: allow user to update image while cropping

* fix: make sure image in the CropView is updated
  • Loading branch information
guoyingtao authored Mar 27, 2024
1 parent fac649d commit e08e341
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
8 changes: 6 additions & 2 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@
<barButtonItem systemItem="bookmarks" id="sQZ-M0-ZLt">
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</barButtonItem>
<barButtonItem systemItem="organize" id="EKa-xq-dX9">
<barButtonItem image="camera.filters" catalog="system" id="EKa-xq-dX9">
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<action selector="updateImage:" destination="aIe-ND-Zfg" id="0bA-SX-yC1"/>
</connections>
</barButtonItem>
<barButtonItem systemItem="action" id="Dzq-Lk-Lna">
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand Down Expand Up @@ -338,9 +341,10 @@
</scene>
</scenes>
<resources>
<image name="camera.filters" catalog="system" width="128" height="119"/>
<image name="sunflower" width="3648" height="5472"/>
<systemColor name="systemGray6Color">
<color red="0.94901960784313721" green="0.94901960784313721" blue="0.96862745098039216" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color red="0.94901960780000005" green="0.94901960780000005" blue="0.96862745100000003" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</systemColor>
<systemColor name="systemYellowColor">
<color red="1" green="0.80000000000000004" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand Down
46 changes: 40 additions & 6 deletions Example/EmbeddedCropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit
import Mantis

class EmbeddedCropViewController: UIViewController {

var image: UIImage?
var cropViewController: CropViewController?

Expand Down Expand Up @@ -63,13 +63,21 @@ class EmbeddedCropViewController: UIViewController {
cropViewController?.crop()
}

@IBAction func updateImage(_ sender: Any) {
guard let image else {
return
}

cropViewController?.update(image.addFilter(filter: .Mono))
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let cropViewController = segue.destination as? CropViewController, let image = image else {
return
}

cropViewController.delegate = self

var config = Mantis.Config()
config.cropToolbarConfig.mode = .embedded
config.enableUndoRedo = true
Expand All @@ -91,8 +99,8 @@ class EmbeddedCropViewController: UIViewController {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {

if action == #selector(EmbeddedCropViewController.undoButtonPressed(_:)) ||
action == #selector(EmbeddedCropViewController.redoButtonPressed(_:)) ||
action == #selector(EmbeddedCropViewController.resetButtonPressed(_:)) {
action == #selector(EmbeddedCropViewController.redoButtonPressed(_:)) ||
action == #selector(EmbeddedCropViewController.resetButtonPressed(_:)) {

return cropViewController!.isUndoSupported()
}
Expand All @@ -105,7 +113,7 @@ class EmbeddedCropViewController: UIViewController {
guard let cropViewController else { return }

if cropViewController.isUndoSupported() {

if command.action == #selector(EmbeddedCropViewController.undoButtonPressed(_:)) {

let undoString = NSLocalizedString("Undo", comment: "Undo")
Expand Down Expand Up @@ -138,7 +146,7 @@ class EmbeddedCropViewController: UIViewController {
}
}
}

}

extension EmbeddedCropViewController: CropViewControllerDelegate {
Expand Down Expand Up @@ -176,3 +184,29 @@ extension EmbeddedCropViewController: CropViewControllerDelegate {
self.resolutionLabel.text = "\(Int(size.width)) x \(Int(size.height)) pixels"
}
}

enum FilterType : String {
case Chrome = "CIPhotoEffectChrome"
case Fade = "CIPhotoEffectFade"
case Instant = "CIPhotoEffectInstant"
case Mono = "CIPhotoEffectMono"
case Noir = "CIPhotoEffectNoir"
case Process = "CIPhotoEffectProcess"
case Tonal = "CIPhotoEffectTonal"
case Transfer = "CIPhotoEffectTransfer"
}

extension UIImage {
func addFilter(filter : FilterType) -> UIImage {
let filter = CIFilter(name: filter.rawValue)
// convert UIImage to CIImage and set as input
let ciInput = CIImage(image: self)
filter?.setValue(ciInput, forKey: "inputImage")
// get output CIImage, render as CGImage first to retain proper UIImage scale
let ciOutput = filter?.outputImage
let ciContext = CIContext()
let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!)
//Return the image
return UIImage(cgImage: cgImage!)
}
}
6 changes: 5 additions & 1 deletion Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ extension CropView {
}

public func makeCropState() -> CropState {

return CropState(
rotationType: viewModel.rotationType,
degrees: viewModel.degrees,
Expand Down Expand Up @@ -1216,6 +1215,11 @@ extension CropView: CropViewProtocol {
viewModel.setRotatingStatus(by: angle)
rotationControlView?.updateRotationValue(by: angle)
}

func update(_ image: UIImage) {
self.image = image
imageContainer.update(image)
}
}

extension UIActivityIndicatorView: ActivityIndicatorProtocol {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Mantis/CropView/ImageContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ extension ImageContainer: ImageContainerProtocol {
bottomLeft: bottomLeft,
bottomRight: bottomRight)
}

func update(_ image: UIImage) {
imageView.image = image
}
}
4 changes: 4 additions & 0 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,10 @@ extension CropViewController {
public func getExpectedCropImageSize() -> CGSize {
cropView.getExpectedCropImageSize()
}

public func update(_ image: UIImage) {
cropView.update(image)
}
}

extension CropViewController: TransformDelegate {
Expand Down
4 changes: 4 additions & 0 deletions Sources/Mantis/Protocols/CropViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ protocol CropViewProtocol: UIView {
func rotate(by angle: Angle)
func makeTransformation() -> Transformation
func makeCropState() -> CropState

func update(_ image: UIImage)
}

extension CropViewProtocol {
Expand All @@ -58,4 +60,6 @@ extension CropViewProtocol {
}

func rotate(by angle: Angle) {}

func update(_ image: UIImage) {}
}
5 changes: 5 additions & 0 deletions Sources/Mantis/Protocols/ImageContainerProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ import UIKit
protocol ImageContainerProtocol: UIView {
func contains(rect: CGRect, fromView view: UIView, tolerance: CGFloat) -> Bool
func getCropRegion(withCropBoxFrame cropBoxFrame: CGRect, cropView: UIView) -> CropRegion
func update(_ image: UIImage)
}

extension ImageContainerProtocol {
func update(_ image: UIImage) {}
}

0 comments on commit e08e341

Please sign in to comment.