Swiftpack.co -  guoyingtao/Mantis as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
guoyingtao/Mantis
A photo cropping tool which mimics Photo.app written by Swift.
.package(url: "https://github.com/guoyingtao/Mantis.git", from: "v1.7.1")

Mantis

swift 5.0 badge platform iOS badge license MIT badge

Mantis

Mantis is an open-source swift library that provides rich cropping interactions for your iOS/Mac app.

Mantis

Mantis also provide rich crop shapes from the basic circle/square to polygon to arbitrary paths(We even provide a heart shape ❤️ 😏).

Mantis

Requirements

  • iOS 11.0+
  • MacOS 10.15+
  • Xcode 10.0+

Install

CocoaPods
pod 'Mantis', '~> 1.7.1'
Carthage
github "guoyingtao/Mantis"
Swift Packages

Usage

Basic
  • Create a cropViewController in Mantis with default config and default mode

You need set (cropViewController or its navigation controller).modalPresentationStyle = .fullscreen for iOS 13+ when the cropViewController is presented

    let cropViewController = Mantis.cropViewController(image: <Your Image>)
    cropViewController.delegate = self
    <Your ViewController>.present(cropViewController, animated: true)
  • The caller needs to conform CropViewControllerDelegate
public protocol CropViewControllerDelegate: class {
    func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation)
    func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage)
    
    // The implementaion of the following functions are optional
    func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage)     
    func cropViewControllerDidBeginResize(_ cropViewController: CropViewController)
    func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo)    
}
UI mode
  • CropViewController has two modes:

    • normal mode

    In normal mode, you can use a set of standard CropViewController photo editing features.

Mantis

let cropViewController = Mantis.cropViewController(image: <Your Image>)
  • customizable mode

This mode includes the standard cropping feature, while enabling users to customize other edit features.

Mantis

let cropViewController = Mantis.cropCustomizableViewController(image: <Your Image>)
Add your own ratio
            // Add a custom ratio 1:2 for portrait orientation
            let config = Mantis.Config()
            config.addCustomRatio(byVerticalWidth: 1, andVerticalHeight: 2)            
            <Your Crop ViewController> = Mantis.cropViewController(image: <Your Image>, config: config)
            
            // Set the ratioOptions of the config if you don't want to keep all default ratios
            let config = Mantis.Config() 
            //config.ratioOptions = [.original, .square, .custom]
            config.ratioOptions = [.custom]
            config.addCustomRatio(byVerticalWidth: 1, andVerticalHeight: 2)            
            <Your Crop ViewController> = Mantis.cropViewController(image: <Your Image>, config: config)
  • If you always want to use only one fixed ratio, set Mantis.Config.presetFixedRatioType = alwaysUsingOnePresetFixedRatio
    <Your Crop ViewController>.config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 16.0 / 9.0)

When choose alwaysUsingOnePresetFixedRatio, fixed-ratio setting button does not show.

  • If you want to hide rotation dial, set Mantis.Config.showRotationDial = false
  • If you want to use ratio list instead of presenter, set Mantis.CropToolbarConfig.ratioCandidatesShowType = .alwaysShowRatioList
public enum RatioCandidatesShowType {
    case presentRatioList
    case alwaysShowRatioList
}
  • If you build your custom toolbar you can add your own fixed ratio buttons
// set a custom fixed ratio
cropToolbarDelegate?.didSelectRatio(ratio: 9 / 16)
Crop shapes
  • If you want to set different crop shape, set Mantis.Config.cropShapeType
public enum CropShapeType {
    case rect
    case square
    case ellipse
    case circle(maskOnly: Bool = false)
    case diamond(maskOnly: Bool = false)
    case heart(maskOnly: Bool = false)
    case polygon(sides: Int, offset: CGFloat = 0, maskOnly: Bool = false)
    case path(points: [CGPoint], maskOnly: Bool = false)
}
Preset transformations
  • If you want to apply transformations when showing an image, set Mantis.Config.presetTransformationType
public enum PresetTransformationType {
    case none
    case presetInfo(info: Transformation)
    case presetNormalizedInfo(normailizedInfo: CGRect)
}

Please use the transformation infomation obtained previously from delegate method cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation).

Demo projects

Mantis provide two demo projects

  • MantisExample (using Storyboard)
  • MantisSwiftUIExample (using SwiftUI)

Credits

GitHub

link
Stars: 298
Last commit: 4 days ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Related Packages

Release Notes

release-please-action v1.7.1
1 week ago

Bug Fixes

  • Add logic for macCatalyst mac idiom (f4c8353)

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API