ImageCropper
ImageCropper 是一个基于 Swift 开发的库。使用 ImageCropper 可以轻松地检测并裁剪人脸、文本、二维码/条形码或矩形(iOS 11 使用 Vision 库,iOS 10 使用 Core Image)。
ImageCropper 是一个纯 Swift 写成的库。使用它可以容易地从给定图片中裁剪出人脸、文本、二维码/条形码或矩形(对于 iOS 11,使用 Vision 库;对于 iOS 10,使用 Core Image)。
特性
- 裁剪图片
- 检测人脸、文本、二维码/条形码或矩形
示例
普通裁剪 | 填充裁剪 |
---|---|
![]() |
![]() |
要求
- iOS 10.0+
- Swift 4.2
快速开始
# CocoaPods
pod 'ImageCropperKit'
# Carthage
github "cp110/ImageCropper"
使用方法
通过简单的方法从你的图像(UIImage或CGImage)中裁剪人脸。
裁剪原始图像是 UIImage 或 CGImage
// `type` in this method can be face, barcode, text or rectangle
// `padding` is the inside margin of the cropped image. default is .zero
// `type`:裁剪类型(人脸、条形码/二维码、文本、方框)
// `paddind`:裁剪出来的图片的内边距(可以使裁剪图片更完整些),默认 .zero
let image = UIImage(named: "image name")
// let image = UIImage(named: "image name")?.cgImage
image?.detector.crop(type: .face, padding: UIEdgeInsets(top: 40, left: 40, bottom: 40, right: 40)) { [weak self] result in
switch result {
case .success(let cropImageResults): // cropImageResults: [(image: T, frame: CGRect)]
// When the `Vision` successfully find type of object you set and successfuly crops it.
print("Found")
case .notFound(let image, let frame):// image: original image (原图)
// When the image doesn't contain any type of object you did set, `result` will be `.notFound`.
print("Not Found")
case .failure(let image, let frame, let error): // image: original image (原图)
// When the any error occured, `result` will be `failure`.
print(error.localizedDescription)
}
}
相关项目
作者
Arthur cp110:[email protected]
许可证
ImageCropper可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。