RSKImageCropViewController.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // RSKImageCropViewController.h
  3. //
  4. // Copyright (c) 2014-present Ruslan Skorb, http://ruslanskorb.com/
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. #import <UIKit/UIKit.h>
  25. NS_ASSUME_NONNULL_BEGIN
  26. @protocol RSKImageCropViewControllerDataSource;
  27. @protocol RSKImageCropViewControllerDelegate;
  28. /**
  29. Types of supported crop modes.
  30. */
  31. typedef NS_ENUM(NSUInteger, RSKImageCropMode) {
  32. RSKImageCropModeCircle,
  33. RSKImageCropModeSquare,
  34. RSKImageCropModeCustom
  35. };
  36. @interface RSKImageCropViewController : UIViewController
  37. /**
  38. Designated initializer. Initializes and returns a newly allocated view controller object with the specified image.
  39. @param originalImage The image for cropping.
  40. */
  41. - (instancetype)initWithImage:(UIImage *)originalImage;
  42. /**
  43. Initializes and returns a newly allocated view controller object with the specified image and the specified crop mode.
  44. @param originalImage The image for cropping.
  45. @param cropMode The mode for cropping.
  46. */
  47. - (instancetype)initWithImage:(UIImage *)originalImage cropMode:(RSKImageCropMode)cropMode;
  48. ///-----------------------------
  49. /// @name Accessing the Delegate
  50. ///-----------------------------
  51. /**
  52. The receiver's delegate.
  53. @discussion A `RSKImageCropViewControllerDelegate` delegate responds to messages sent by completing / canceling crop the image in the image crop view controller.
  54. */
  55. @property (weak, nonatomic, nullable) id<RSKImageCropViewControllerDelegate> delegate;
  56. /**
  57. The receiver's data source.
  58. @discussion A `RSKImageCropViewControllerDataSource` data source provides a custom rect and a custom path for the mask and a custom movement rect for the image.
  59. */
  60. @property (weak, nonatomic, nullable) id<RSKImageCropViewControllerDataSource> dataSource;
  61. ///--------------------------
  62. /// @name Accessing the Image
  63. ///--------------------------
  64. /**
  65. The image for cropping.
  66. */
  67. @property (strong, nonatomic) UIImage *originalImage;
  68. /// -----------------------------------
  69. /// @name Accessing the Mask Attributes
  70. /// -----------------------------------
  71. /**
  72. The color of the layer with the mask. Default value is [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.7f].
  73. */
  74. @property (copy, nonatomic) UIColor *maskLayerColor;
  75. /**
  76. The line width used when stroking the path of the mask layer. Default value is 1.0.
  77. */
  78. @property (assign, nonatomic) CGFloat maskLayerLineWidth;
  79. /**
  80. The color to fill the stroked outline of the path of the mask layer, or nil for no stroking. Default valus is nil.
  81. */
  82. @property (copy, nonatomic, nullable) UIColor *maskLayerStrokeColor;
  83. /**
  84. The rect of the mask.
  85. @discussion Updating each time before the crop view lays out its subviews.
  86. */
  87. @property (assign, readonly, nonatomic) CGRect maskRect;
  88. /**
  89. The path of the mask.
  90. @discussion Updating each time before the crop view lays out its subviews.
  91. */
  92. @property (copy, readonly, nonatomic) UIBezierPath *maskPath;
  93. /// -----------------------------------
  94. /// @name Accessing the Crop Attributes
  95. /// -----------------------------------
  96. /**
  97. The mode for cropping. Default value is `RSKImageCropModeCircle`.
  98. */
  99. @property (assign, nonatomic) RSKImageCropMode cropMode;
  100. /**
  101. The crop rectangle.
  102. @discussion The value is calculated at run time.
  103. */
  104. @property (readonly, nonatomic) CGRect cropRect;
  105. /**
  106. A value that specifies the current rotation angle of the image in radians.
  107. @discussion The value is calculated at run time.
  108. */
  109. @property (readonly, nonatomic) CGFloat rotationAngle;
  110. /**
  111. A floating-point value that specifies the current scale factor applied to the image.
  112. @discussion The value is calculated at run time.
  113. */
  114. @property (readonly, nonatomic) CGFloat zoomScale;
  115. /**
  116. A Boolean value that determines whether the image will always fill the mask space. Default value is `NO`.
  117. */
  118. @property (assign, nonatomic) BOOL avoidEmptySpaceAroundImage;
  119. /**
  120. A Boolean value that determines whether the image will always bounce horizontally. Default value is `NO`.
  121. */
  122. @property (assign, nonatomic) BOOL alwaysBounceHorizontal;
  123. /**
  124. A Boolean value that determines whether the image will always bounce vertically. Default value is `NO`.
  125. */
  126. @property (assign, nonatomic) BOOL alwaysBounceVertical;
  127. /**
  128. A Boolean value that determines whether the mask applies to the image after cropping. Default value is `NO`.
  129. */
  130. @property (assign, nonatomic) BOOL applyMaskToCroppedImage;
  131. /**
  132. A Boolean value that controls whether the rotaion gesture is enabled. Default value is `NO`.
  133. @discussion To support the rotation when `cropMode` is `RSKImageCropModeCustom` you must implement the data source method `imageCropViewControllerCustomMovementRect:`.
  134. */
  135. @property (assign, getter=isRotationEnabled, nonatomic) BOOL rotationEnabled;
  136. /// -------------------------------
  137. /// @name Accessing the UI Elements
  138. /// -------------------------------
  139. /**
  140. The Title Label.
  141. */
  142. @property (strong, nonatomic, readonly) UILabel *moveAndScaleLabel;
  143. /**
  144. The Cancel Button.
  145. */
  146. @property (strong, nonatomic, readonly) UIButton *cancelButton;
  147. /**
  148. The Choose Button.
  149. */
  150. @property (strong, nonatomic, readonly) UIButton *chooseButton;
  151. /// -------------------------------------------
  152. /// @name Checking of the Interface Orientation
  153. /// -------------------------------------------
  154. /**
  155. Returns a Boolean value indicating whether the user interface is currently presented in a portrait orientation.
  156. @return YES if the interface orientation is portrait, otherwise returns NO.
  157. */
  158. - (BOOL)isPortraitInterfaceOrientation;
  159. /// -------------------------------------
  160. /// @name Accessing the Layout Attributes
  161. /// -------------------------------------
  162. /**
  163. The inset of the circle mask rect's area within the crop view's area in portrait orientation. Default value is `15.0f`.
  164. */
  165. @property (assign, nonatomic) CGFloat portraitCircleMaskRectInnerEdgeInset;
  166. /**
  167. The inset of the square mask rect's area within the crop view's area in portrait orientation. Default value is `20.0f`.
  168. */
  169. @property (assign, nonatomic) CGFloat portraitSquareMaskRectInnerEdgeInset;
  170. /**
  171. The vertical space between the top of the 'Move and Scale' label and the top of the crop view in portrait orientation. Default value is `64.0f`.
  172. */
  173. @property (assign, nonatomic) CGFloat portraitMoveAndScaleLabelTopAndCropViewTopVerticalSpace;
  174. /**
  175. The vertical space between the bottom of the crop view and the bottom of the 'Cancel' button in portrait orientation. Default value is `21.0f`.
  176. */
  177. @property (assign, nonatomic) CGFloat portraitCropViewBottomAndCancelButtonBottomVerticalSpace;
  178. /**
  179. The vertical space between the bottom of the crop view and the bottom of the 'Choose' button in portrait orientation. Default value is `21.0f`.
  180. */
  181. @property (assign, nonatomic) CGFloat portraitCropViewBottomAndChooseButtonBottomVerticalSpace;
  182. /**
  183. The horizontal space between the leading of the 'Cancel' button and the leading of the crop view in portrait orientation. Default value is `13.0f`.
  184. */
  185. @property (assign, nonatomic) CGFloat portraitCancelButtonLeadingAndCropViewLeadingHorizontalSpace;
  186. /**
  187. The horizontal space between the trailing of the crop view and the trailing of the 'Choose' button in portrait orientation. Default value is `13.0f`.
  188. */
  189. @property (assign, nonatomic) CGFloat portraitCropViewTrailingAndChooseButtonTrailingHorizontalSpace;
  190. /**
  191. The inset of the circle mask rect's area within the crop view's area in landscape orientation. Default value is `45.0f`.
  192. */
  193. @property (assign, nonatomic) CGFloat landscapeCircleMaskRectInnerEdgeInset;
  194. /**
  195. The inset of the square mask rect's area within the crop view's area in landscape orientation. Default value is `45.0f`.
  196. */
  197. @property (assign, nonatomic) CGFloat landscapeSquareMaskRectInnerEdgeInset;
  198. /**
  199. The vertical space between the top of the 'Move and Scale' label and the top of the crop view in landscape orientation. Default value is `12.0f`.
  200. */
  201. @property (assign, nonatomic) CGFloat landscapeMoveAndScaleLabelTopAndCropViewTopVerticalSpace;
  202. /**
  203. The vertical space between the bottom of the crop view and the bottom of the 'Cancel' button in landscape orientation. Default value is `12.0f`.
  204. */
  205. @property (assign, nonatomic) CGFloat landscapeCropViewBottomAndCancelButtonBottomVerticalSpace;
  206. /**
  207. The vertical space between the bottom of the crop view and the bottom of the 'Choose' button in landscape orientation. Default value is `12.0f`.
  208. */
  209. @property (assign, nonatomic) CGFloat landscapeCropViewBottomAndChooseButtonBottomVerticalSpace;
  210. /**
  211. The horizontal space between the leading of the 'Cancel' button and the leading of the crop view in landscape orientation. Default value is `13.0f`.
  212. */
  213. @property (assign, nonatomic) CGFloat landscapeCancelButtonLeadingAndCropViewLeadingHorizontalSpace;
  214. /**
  215. The horizontal space between the trailing of the crop view and the trailing of the 'Choose' button in landscape orientation. Default value is `13.0f`.
  216. */
  217. @property (assign, nonatomic) CGFloat landscapeCropViewTrailingAndChooseButtonTrailingHorizontalSpace;
  218. @end
  219. /**
  220. The `RSKImageCropViewControllerDataSource` protocol is adopted by an object that provides a custom rect and a custom path for the mask and a custom movement rect for the image.
  221. */
  222. @protocol RSKImageCropViewControllerDataSource <NSObject>
  223. /**
  224. Asks the data source a custom rect for the mask.
  225. @param controller The crop view controller object to whom a rect is provided.
  226. @return A custom rect for the mask.
  227. */
  228. - (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller;
  229. /**
  230. Asks the data source a custom path for the mask.
  231. @param controller The crop view controller object to whom a path is provided.
  232. @return A custom path for the mask.
  233. */
  234. - (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller;
  235. /**
  236. Asks the data source a custom rect in which the image can be moved.
  237. @param controller The crop view controller object to whom a rect is provided.
  238. @return A custom rect in which the image can be moved.
  239. */
  240. - (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller;
  241. @end
  242. /**
  243. The `RSKImageCropViewControllerDelegate` protocol defines messages sent to a image crop view controller delegate when crop image was canceled or the original image was cropped.
  244. */
  245. @protocol RSKImageCropViewControllerDelegate <NSObject>
  246. /**
  247. Tells the delegate that crop image has been canceled.
  248. */
  249. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller;
  250. /**
  251. Tells the delegate that the original image has been cropped. Additionally provides a crop rect and a rotation angle used to produce image.
  252. */
  253. - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle;
  254. @optional
  255. /**
  256. Tells the delegate that the original image will be cropped.
  257. */
  258. - (void)imageCropViewController:(RSKImageCropViewController *)controller willCropImage:(UIImage *)originalImage;
  259. @end
  260. NS_ASSUME_NONNULL_END