Code Monkey home page Code Monkey logo

mantis's Introduction

Mantis

swift 5.0 badge platform iOS badge license MIT badge

Mantis

Mantis is an iOS Image cropping library, which mimics the Photo App written in Swift and provides rich cropping interactions for your iOS/Mac app (Catalyst only).

Mantis

Mantis also provides 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.9.0'
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, cropInfo: CropInfo)
    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, , cropInfo: CropInfo).

Localization
  • UIKit project
    Add more languages support to the Localizaions section for Project Info tab

Mantis
fig 1

  • SwiftUI project
    Please check this link

  • Static frameworks If you use static frameworks in CocoaPods, you need to add the code below in order to find the correct resource bundle.

    Mantis.locateResourceBundle(by: Self.self)
  • Custom localization tables and bundle

By default mantis will use built in localization tables to get string resources not every language is supported out of the box (see fig 1).

However if your app support multiple languages and those languages are not 'built in', then you can define your own strings table and localize them in the application target or framework. By doing so you'll need to configure Mantis localization.

IMPORTANT! Firstly you'll need to create strings file with these keys:

"Mantis.Done" = "";
"Mantis.Cancel" = "";
"Mantis.Reset" = "";
"Mantis.Original" = "";
"Mantis.Square" = "";

Then you'll need to configure Mantis:

let config = Mantis.Config()
config.localizationConfig.bundle = // a bundle where strings file is located
config.localizationConfig.tableName = // a localizaed strings file name within the bundle

Demo projects

Mantis provide two demo projects

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

Credits

mantis's People

Contributors

7bluexy avatar aldrnikolajev avatar codebeauty avatar danielsaidi avatar github-actions[bot] avatar guoyingtao avatar huanghui1998hhh avatar hyouuu avatar ibinh avatar levanlongktmt avatar luke-oneill avatar markspit avatar natashatherobot avatar nikolayjuly avatar nixzhu avatar timbaev avatar tinder-sgong avatar vanniktech avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.