一款适用于 iOS 的图像裁剪器,类似于“通讯录”应用中的裁剪器,并支持横向方向。
RSKImageCropper 需要 iOS 12.0 或更高版本。
要将 RSKImageCropper
包添加到您的 Xcode 项目,请选择“File” > “Swift Packages” > “Add Package Dependency”,然后输入仓库 URL。
https://github.com/ruslanskorb/RSKImageCropper.git
将 pod RSKImageCropper
添加到您的 Podfile。
pod 'RSKImageCropper'
从终端运行 pod install
,然后打开您应用的 .xcworkspace
文件以启动 Xcode。
导入 RSKImageCropper.h
头文件。通常,这应写为 #import <RSKImageCropper/RSKImageCropper.h>
将 ruslanskorb/RSKImageCropper
项目添加到您的 Cartfile。
github "ruslanskorb/RSKImageCropper"
运行 carthage update
,然后按照所需的其他步骤将 iOS 和/或 Mac 框架添加到您的项目中。
导入 RSKImageCropper 框架/模块。
@import RSKImageCropper
#import <RSKImageCropper/RSKImageCropper.h>
导入类头文件。
#import <RSKImageCropper/RSKImageCropper.h>
只需创建一个用于图像裁剪的视图控制器并设置委托。
- (IBAction)onButtonTouch:(UIButton *)sender
{
UIImage *image = [UIImage imageNamed:@"image"];
RSKImageCropViewController *imageCropViewController = [[RSKImageCropViewController alloc] initWithImage:image];
imageCropViewController.delegate = self;
[self.navigationController pushViewController:imageCropViewController animated:YES];
}
RSKImageCropViewControllerDelegate
提供了三个委托方法。要使用它们,请在您的视图控制器中实现委托。
@interface ViewController () <RSKImageCropViewControllerDelegate>
然后实现委托函数。
// Crop image has been canceled.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
[self.navigationController popViewControllerAnimated:YES];
}
// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
rotationAngle:(CGFloat)rotationAngle
{
self.imageView.image = croppedImage;
[self.navigationController popViewControllerAnimated:YES];
}
// The original image will be cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
willCropImage:(UIImage *)originalImage
{
// Use when `applyMaskToCroppedImage` set to YES.
[SVProgressHUD show];
}
RSKImageCropViewControllerDataSource
提供了三个数据源方法。方法 imageCropViewControllerCustomMaskRect:
请求数据源提供自定义的蒙版矩形。方法 imageCropViewControllerCustomMaskPath:
请求数据源提供自定义的蒙版路径。方法 imageCropViewControllerCustomMovementRect:
请求数据源提供图像可以移动的自定义矩形。要使用它们,请在您的视图控制器中实现数据源。
@interface ViewController () <RSKImageCropViewControllerDataSource>
然后实现数据源函数。
// Returns a custom rect for the mask.
- (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller
{
CGSize aspectRatio = CGSizeMake(16.0f, 9.0f);
CGFloat viewWidth = CGRectGetWidth(controller.view.frame);
CGFloat viewHeight = CGRectGetHeight(controller.view.frame);
CGFloat maskWidth;
if ([controller isPortraitInterfaceOrientation]) {
maskWidth = viewWidth;
} else {
maskWidth = viewHeight;
}
CGFloat maskHeight;
do {
maskHeight = maskWidth * aspectRatio.height / aspectRatio.width;
maskWidth -= 1.0f;
} while (maskHeight != floor(maskHeight));
maskWidth += 1.0f;
CGSize maskSize = CGSizeMake(maskWidth, maskHeight);
CGRect maskRect = CGRectMake((viewWidth - maskSize.width) * 0.5f,
(viewHeight - maskSize.height) * 0.5f,
maskSize.width,
maskSize.height);
return maskRect;
}
// Returns a custom path for the mask.
- (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller
{
CGRect rect = controller.maskRect;
CGPoint point1 = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
CGPoint point2 = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
CGPoint point3 = CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGPoint point4 = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
UIBezierPath *rectangle = [UIBezierPath bezierPath];
[rectangle moveToPoint:point1];
[rectangle addLineToPoint:point2];
[rectangle addLineToPoint:point3];
[rectangle addLineToPoint:point4];
[rectangle closePath];
return rectangle;
}
// Returns a custom rect in which the image can be moved.
- (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller
{
if (controller.rotationAngle == 0) {
return controller.maskRect;
} else {
CGRect maskRect = controller.maskRect;
CGFloat rotationAngle = controller.rotationAngle;
CGRect movementRect = CGRectZero;
movementRect.size.width = CGRectGetWidth(maskRect) * fabs(cos(rotationAngle)) + CGRectGetHeight(maskRect) * fabs(sin(rotationAngle));
movementRect.size.height = CGRectGetHeight(maskRect) * fabs(cos(rotationAngle)) + CGRectGetWidth(maskRect) * fabs(sin(rotationAngle));
movementRect.origin.x = CGRectGetMinX(maskRect) + (CGRectGetWidth(maskRect) - CGRectGetWidth(movementRect)) * 0.5f;
movementRect.origin.y = CGRectGetMinY(maskRect) + (CGRectGetHeight(maskRect) - CGRectGetHeight(movementRect)) * 0.5f;
movementRect.origin.x = floor(CGRectGetMinX(movementRect));
movementRect.origin.y = floor(CGRectGetMinY(movementRect));
movementRect = CGRectIntegral(movementRect);
return movementRect;
}
}
在 Xcode 中构建并运行 RSKImageCropperExample
项目,以查看 RSKImageCropper
的实际效果。玩得开心。Fork 并发送 pull request。找出用于自定义的 hooks。
RSKImageCropper
不需要隐私清单。根据从 Apple 收到的信息,我们应避免向我们的框架添加空的隐私清单。
Ruslan Skorb
本项目根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。感谢通过链接到项目页面进行署名。