类似于通讯录应用中的图像裁剪器,支持横幅方向。
安装
RSKImageCropper 需要 iOS 12.0 或更高版本。
Swift 包管理器
使用-
要将
RSKImageCropper
包添加到您的 Xcode 项目中,请选择“文件 > Swift 包 > 添加包依赖项”并输入仓库 URL。https://github.com/ruslanskorb/RSKImageCropper.git
CocoaPods
使用-
将
RSKImageCropper
pod 添加到您的 Podfile 中。pod 'RSKImageCropper'
-
从终端运行
pod install
,然后打开您的应用的.xcworkspace
文件以启动 Xcode。 -
导入
RSKImageCropper.h
头文件。通常,应该写成#import <RSKImageCropper/RSKImageCropper.h>
Carthage
使用-
将
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 请求。找出定制的钩子。
联系
Ruslan Skorb
许可证
此项目可供使用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。我们感激通过链接到 项目页面 进行归因。