
iOS的圖像農作物類似於Contacts App中的iOS,並支持景觀取向。
rskimagecropper需要iOS 12.0或更高版本。
要將RSKImageCropper軟件包添加到您的Xcode項目中,請選擇“文件> swift軟件包”>“添加軟件包依賴關係”並輸入存儲庫URL。
https://github.com/ruslanskorb/RSKImageCropper.git
將POD RSKImageCropper添加到您的podfile中。
pod 'RSKImageCropper'
從終端運行pod install ,然後打開您的應用程序的.xcworkspace文件以啟動Xcode。
導入RSKImageCropper.h頭。通常,這應該寫為#import <RSKImageCropper/RSKImageCropper.h>
將ruslanskorb/RSKImageCropper項目添加到您的卡特文件中。
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:向掩模的自定義RECT詢問數據源。方法imageCropViewControllerCustomMaskPath:向數據源詢問掩碼的自定義路徑。方法imageCropViewControllerCustomMovementRect:向數據源詢問可以移動圖像的自定義RECT。要使用它們,請在視圖控制器中實現數據源。
@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的作用。玩得開心。分叉並發送拉請請求。找出自定義的鉤子。
RSKImageCropper不需要隱私清單。根據蘋果收到的信息,我們應該避免在我們的框架中添加空的隱私清單。
Ruslan Skorb
該項目可根據麻省理工學院許可獲得。有關更多信息,請參見許可證文件。通過鏈接到項目頁面來歸因。