测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2015年6月 |
由 Alexandre Garrefa 维护。
一个类似于“联系人”应用中的图像裁剪器,支持横版模式的 iOS 图像裁剪器。
CocoaPods 是推荐安装 RSKImageCropper 的方法。只需将以下行添加到您的 Podfile
pod 'RSKImageCropper'
导入类头。
#import "RSKImageCropViewController.h"
创建一个用于图像裁剪的视图控制器并设置代理。
- (IBAction)onButtonTouch:(UIButton *)sender
{
UIImage *image = [UIImage imageNamed:@"image"];
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image];
imageCropVC.delegate = self;
[self.navigationController pushViewController:imageCropVC 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.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
{
self.imageView.image = croppedImage;
[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 maskSize;
if ([controller isPortraitInterfaceOrientation]) {
maskSize = CGSizeMake(250, 250);
} else {
maskSize = CGSizeMake(220, 220);
}
CGFloat viewWidth = CGRectGetWidth(controller.view.frame);
CGFloat viewHeight = CGRectGetHeight(controller.view.frame);
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(CGRectGetMidX(rect), CGRectGetMinY(rect));
UIBezierPath *triangle = [UIBezierPath bezierPath];
[triangle moveToPoint:point1];
[triangle addLineToPoint:point2];
[triangle addLineToPoint:point3];
[triangle closePath];
return triangle;
}
// Returns a custom rect in which the image can be moved.
- (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller
{
// If the image is not rotated, then the movement rect coincides with the mask rect.
return controller.maskRect;
}
在 Xcode 中构建并运行 RSKImageCropperExample
项目,以查看 RSKImageCropper 的实际应用。祝您玩得开心。克隆并发送拉取请求。找出自定义的挂钩。
Ruslan Skorb
此项目可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。通过链接到 项目页面 进行归因是受欢迎的。