You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
7.4 KiB

  1. ## RSKImageCropper [![Build Status](https://travis-ci.org/ruslanskorb/RSKImageCropper.svg)](https://travis-ci.org/ruslanskorb/RSKImageCropper) [![Coverage Status](https://coveralls.io/repos/ruslanskorb/RSKImageCropper/badge.svg)](https://coveralls.io/r/ruslanskorb/RSKImageCropper) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/ruslanskorb/RSKImageCropper)
  2. <p align="center">
  3. <img src="Screenshot.png" alt="Sample">
  4. </p>
  5. An image cropper for iOS like in the Contacts app with support for landscape orientation.
  6. ## Installation
  7. *RSKImageCropper requires iOS 6.0 or later.*
  8. ### Using [CocoaPods](http://cocoapods.org)
  9. 1. Add the pod `RSKImageCropper` to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html).
  10. pod 'RSKImageCropper'
  11. 2. Run `pod install` from Terminal, then open your app's `.xcworkspace` file to launch Xcode.
  12. 3. Import the `RSKImageCropper.h` header. Typically, this should be written as `#import <RSKImageCropper/RSKImageCropper.h>`
  13. ### Using [Carthage](https://github.com/Carthage/Carthage)
  14. 1. Add the `ruslanskorb/RSKImageCropper` project to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile).
  15. github "ruslanskorb/RSKImageCropper"
  16. 2. Run `carthage update`, then follow the [additional steps required](https://github.com/Carthage/Carthage#adding-frameworks-to-an-application) to add the iOS and/or Mac frameworks into your project.
  17. 3. Import the RSKImageCropper framework/module.
  18. * Using Modules: `@import RSKImageCropper`
  19. * Without Modules: `#import <RSKImageCropper/RSKImageCropper.h>`
  20. ## Basic Usage
  21. Import the class header.
  22. ``` objective-c
  23. #import <RSKImageCropper/RSKImageCropper.h>
  24. ```
  25. Just create a view controller for image cropping and set the delegate.
  26. ``` objective-c
  27. - (IBAction)onButtonTouch:(UIButton *)sender
  28. {
  29. UIImage *image = [UIImage imageNamed:@"image"];
  30. RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:image];
  31. imageCropVC.delegate = self;
  32. [self.navigationController pushViewController:imageCropVC animated:YES];
  33. }
  34. ```
  35. ## Delegate
  36. `RSKImageCropViewControllerDelegate` provides three delegate methods. To use them, implement the delegate in your view controller.
  37. ```objective-c
  38. @interface ViewController () <RSKImageCropViewControllerDelegate>
  39. ```
  40. Then implement the delegate functions.
  41. ```objective-c
  42. // Crop image has been canceled.
  43. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
  44. {
  45. [self.navigationController popViewControllerAnimated:YES];
  46. }
  47. // The original image has been cropped. Additionally provides a rotation angle used to produce image.
  48. - (void)imageCropViewController:(RSKImageCropViewController *)controller
  49. didCropImage:(UIImage *)croppedImage
  50. usingCropRect:(CGRect)cropRect
  51. rotationAngle:(CGFloat)rotationAngle
  52. {
  53. self.imageView.image = croppedImage;
  54. [self.navigationController popViewControllerAnimated:YES];
  55. }
  56. // The original image will be cropped.
  57. - (void)imageCropViewController:(RSKImageCropViewController *)controller
  58. willCropImage:(UIImage *)originalImage
  59. {
  60. // Use when `applyMaskToCroppedImage` set to YES.
  61. [SVProgressHUD show];
  62. }
  63. ```
  64. ## DataSource
  65. `RSKImageCropViewControllerDataSource` provides three data source methods. The method `imageCropViewControllerCustomMaskRect:` asks the data source a custom rect for the mask. The method `imageCropViewControllerCustomMaskPath:` asks the data source a custom path for the mask. The method `imageCropViewControllerCustomMovementRect:` asks the data source a custom rect in which the image can be moved. To use them, implement the data source in your view controller.
  66. ```objective-c
  67. @interface ViewController () <RSKImageCropViewControllerDataSource>
  68. ```
  69. Then implement the data source functions.
  70. ```objective-c
  71. // Returns a custom rect for the mask.
  72. - (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller
  73. {
  74. CGSize aspectRatio = CGSizeMake(16.0f, 9.0f);
  75. CGFloat viewWidth = CGRectGetWidth(controller.view.frame);
  76. CGFloat viewHeight = CGRectGetHeight(controller.view.frame);
  77. CGFloat maskWidth;
  78. if ([controller isPortraitInterfaceOrientation]) {
  79. maskWidth = viewWidth;
  80. } else {
  81. maskWidth = viewHeight;
  82. }
  83. CGFloat maskHeight;
  84. do {
  85. maskHeight = maskWidth * aspectRatio.height / aspectRatio.width;
  86. maskWidth -= 1.0f;
  87. } while (maskHeight != floor(maskHeight));
  88. maskWidth += 1.0f;
  89. CGSize maskSize = CGSizeMake(maskWidth, maskHeight);
  90. CGRect maskRect = CGRectMake((viewWidth - maskSize.width) * 0.5f,
  91. (viewHeight - maskSize.height) * 0.5f,
  92. maskSize.width,
  93. maskSize.height);
  94. return maskRect;
  95. }
  96. // Returns a custom path for the mask.
  97. - (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller
  98. {
  99. CGRect rect = controller.maskRect;
  100. CGPoint point1 = CGPointMake(CGRectGetMinX(rect), CGRectGetMaxY(rect));
  101. CGPoint point2 = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));
  102. CGPoint point3 = CGPointMake(CGRectGetMaxX(rect), CGRectGetMinY(rect));
  103. CGPoint point4 = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
  104. UIBezierPath *rectangle = [UIBezierPath bezierPath];
  105. [rectangle moveToPoint:point1];
  106. [rectangle addLineToPoint:point2];
  107. [rectangle addLineToPoint:point3];
  108. [rectangle addLineToPoint:point4];
  109. [rectangle closePath];
  110. return rectangle;
  111. }
  112. // Returns a custom rect in which the image can be moved.
  113. - (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller
  114. {
  115. if (controller.rotationAngle == 0) {
  116. return controller.maskRect;
  117. } else {
  118. CGRect maskRect = controller.maskRect;
  119. CGFloat rotationAngle = controller.rotationAngle;
  120. CGRect movementRect = CGRectZero;
  121. movementRect.size.width = CGRectGetWidth(maskRect) * fabs(cos(rotationAngle)) + CGRectGetHeight(maskRect) * fabs(sin(rotationAngle));
  122. movementRect.size.height = CGRectGetHeight(maskRect) * fabs(cos(rotationAngle)) + CGRectGetWidth(maskRect) * fabs(sin(rotationAngle));
  123. movementRect.origin.x = CGRectGetMinX(maskRect) + (CGRectGetWidth(maskRect) - CGRectGetWidth(movementRect)) * 0.5f;
  124. movementRect.origin.y = CGRectGetMinY(maskRect) + (CGRectGetHeight(maskRect) - CGRectGetHeight(movementRect)) * 0.5f;
  125. movementRect.origin.x = floor(CGRectGetMinX(movementRect));
  126. movementRect.origin.y = floor(CGRectGetMinY(movementRect));
  127. movementRect = CGRectIntegral(movementRect);
  128. return movementRect;
  129. }
  130. }
  131. ```
  132. ## Coming Soon
  133. - If you would like to request a new feature, feel free to raise as an issue.
  134. ## Demo
  135. Build and run the `RSKImageCropperExample` project in Xcode to see `RSKImageCropper` in action.
  136. Have fun. Fork and send pull requests. Figure out hooks for customization.
  137. ## Contact
  138. Ruslan Skorb
  139. - http://github.com/ruslanskorb
  140. - http://twitter.com/ruslanskorb
  141. - ruslan.skorb@gmail.com
  142. ## License
  143. This project is is available under the MIT license. See the LICENSE file for more info. Attribution by linking to the [project page](https://github.com/ruslanskorb/RSKImageCropper) is appreciated.