Mantis 2.22.0

Mantis 2.22.0

郭英涛维护。



Mantis 2.22.0

Mantis

swift 5.0 badge platform iOS badge license MIT badge

Mantis

Mantis是一个iOS图像裁剪库,它模仿Swift编写的照片应用,并为您的iOS/Mac应用(仅Catalyst)提供丰富的裁剪交互。

Mantis RotaionDial Mantis SlideDial

示例

Mantis Normal Demos Mantis RotaionDial Demos Mantis SlideDial Demos

Mantis还提供了从基本的圆形/方形到多边形再到任意路径的丰富裁剪形状(我们甚至还提供了心形❤️ 😏).

Mantis

要求

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

2.x.x版本的破坏性更改

  • 添加CropViewConfig
    • 将一些属性从Config移动到CropViewConfig
    • 将 rotationControlViewConfig 作为 CropViewConfig 的属性
  • 重构 CropToolbarConfigProtocol
    • 重命名一些属性

安装

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

使用

基础
  • 使用默认配置和默认模式在 Mantis 中创建一个 cropViewController

当 cropViewController 被展示时,调用者需要设置 (cropViewController 或其 navigation controller).modalPresentationStyle = .fullscreen 以支持 iOS 13+

    let cropViewController = Mantis.cropViewController(image: <Your Image>)
    cropViewController.delegate = self
    <Your ViewController>.present(cropViewController, animated: true)
  • 调用者需要遵循 CropViewControllerDelegate
public protocol CropViewControllerDelegate: class {
    func cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, cropInfo: CropInfo)
    func cropViewControllerDidCancel(_ cropViewController: CropViewController, original: UIImage)
    
    // The implementation of the following functions are optional
    func cropViewControllerDidFailToCrop(_ cropViewController: CropViewController, original: UIImage)     
    func cropViewControllerDidBeginResize(_ cropViewController: CropViewController)
    func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo)    
}
裁剪工具栏模式
  • CropToolbar 有两种模式

    • 普通模式

    在普通模式下,你可以使用一系列标准的 CropViewController 照片编辑功能,包括 "取消" 和 "确定" 按钮。

Mantis

let cropViewController = Mantis.cropViewController(image: <Your Image>)
  • 嵌入式模式

此模式不包括 "取消" 和 "确定" 按钮,因此你可以将 CropViewController 嵌入到另一个视图控制器中

Mantis

var config = Mantis.Config()
config.cropToolbarConfig.mode = .embedded
let cropViewController = Mantis.cropViewController(image: <Your Image>, config: config)
添加您的比例
            // 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)
  • 如果您总是希望只使用一个固定的比例,请设置 Mantis.Config.presetFixedRatioType = alwaysUsingOnePresetFixedRatio
    <Your Crop ViewController>.config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 16.0 / 9.0)

当选择 alwaysUsingOnePresetFixedRatio 时,不会显示固定比例设置按钮。

  • 如果您想隐藏旋转控制视图,请设置 Mantis.Config.cropViewConfig.showAttachedRotationControlView = false
  • 如果您想使用比例列表而不是主持人,请设置 Mantis.CropToolbarConfig.ratioCandidatesShowType = .alwaysShowRatioList
public enum RatioCandidatesShowType {
    case presentRatioList
    case alwaysShowRatioList
}
  • 如果您构建自己的自定义工具栏,可以添加您自己的固定比例按钮
// set a custom fixed ratio
cropToolbarDelegate?.didSelectRatio(ratio: 9 / 16)
裁剪形状
  • 如果您想设置不同的裁剪形状,请设置 Mantis.Config.cropViewConfig.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)
}
预设转换
  • 如果您想在展示图片时应用转换,请设置 Mantis.Config.cropViewConfig.presetTransformationType
public enum PresetTransformationType {
    case none
    case presetInfo(info: Transformation)
    case presetNormalizedInfo(normalizedInfo: CGRect)
}

请使用从代理方法 cropViewControllerDidCrop(_ cropViewController: CropViewController, cropped: UIImage, transformation: Transformation, , cropInfo: CropInfo) 获得的先前转换信息。

本地化
  • UIKit 项目
    为项目信息标签的本地化部分添加更多语言支持

Mantis
图 1

  • SwiftUI 项目
    请参阅此链接 链接

  • 静态框架 如果您在 CocoaPods 中使用静态框架,您需要添加以下代码以找到正确的资源包。

    Mantis.locateResourceBundle(by: Self.self)
  • 自定义本地化表和包

默认情况下,mantis 会使用内置的本地化表格来获取字符串资源,并非所有语言都支持开箱即用(见图 1)。

但是如果您应用程序支持多种语言且这些语言不是“内置”的,那么您可以在应用程序目标或框架中定义自己的字符串表并对其进行本地化。通过这样做,您需要配置Mantis本地化。

重要!首先,您需要创建包含以下键的字符串文件

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

然后您需要配置Mantis

let config = Mantis.Config()
config.localizationConfig.bundle = // a bundle where strings file is located
config.localizationConfig.tableName = // a localized strings file name within the bundle
自定义视图控制器
  • 如果需要,您可以子类化CropViewController
class CustomViewController: CropViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do your custom logic here.
        // The MantisExample project also has a showcase for a CustomViewController.
    }
}
  • 要获取实例,Mantis提供了一个工厂方法
let cropViewController: CustomViewController = Mantis.cropViewController(image: image, config: config)

演示项目

Mantis提供了两个演示项目

  • MantisExample(使用Storyboard)
  • MantisSwiftUIExample(使用SwiftUI)

鸣谢