RSKImageCropViewController.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. Zooms to a specific area of the image so that it is visible.
  50. @param rect A rectangle defining an area of the image.
  51. @param animated YES if the scrolling should be animated, NO if it should be immediate.
  52. */
  53. - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated;
  54. ///-----------------------------
  55. /// @name Accessing the Delegate
  56. ///-----------------------------
  57. /**
  58. The receiver's delegate.
  59. @discussion A `RSKImageCropViewControllerDelegate` delegate responds to messages sent by completing / canceling crop the image in the image crop view controller.
  60. */
  61. @property (weak, nonatomic, nullable) id<RSKImageCropViewControllerDelegate> delegate;
  62. /**
  63. The receiver's data source.
  64. @discussion A `RSKImageCropViewControllerDataSource` data source provides a custom rect and a custom path for the mask and a custom movement rect for the image.
  65. */
  66. @property (weak, nonatomic, nullable) id<RSKImageCropViewControllerDataSource> dataSource;
  67. ///--------------------------
  68. /// @name Accessing the Image
  69. ///--------------------------
  70. /**
  71. The image for cropping.
  72. */
  73. @property (strong, nonatomic) UIImage *originalImage;
  74. /// -----------------------------------
  75. /// @name Accessing the Mask Attributes
  76. /// -----------------------------------
  77. /**
  78. The color of the layer with the mask. Default value is [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.7f].
  79. */
  80. @property (copy, nonatomic) UIColor *maskLayerColor;
  81. /**
  82. The line width used when stroking the path of the mask layer. Default value is 1.0.
  83. */
  84. @property (assign, nonatomic) CGFloat maskLayerLineWidth;
  85. /**
  86. The color to fill the stroked outline of the path of the mask layer, or nil for no stroking. Default valus is nil.
  87. */
  88. @property (copy, nonatomic, nullable) UIColor *maskLayerStrokeColor;
  89. /**
  90. The rect of the mask.
  91. @discussion Updating each time before the crop view lays out its subviews.
  92. */
  93. @property (assign, readonly, nonatomic) CGRect maskRect;
  94. /**
  95. The path of the mask.
  96. @discussion Updating each time before the crop view lays out its subviews.
  97. */
  98. @property (copy, readonly, nonatomic) UIBezierPath *maskPath;
  99. /// -----------------------------------
  100. /// @name Accessing the Crop Attributes
  101. /// -----------------------------------
  102. /**
  103. The mode for cropping. Default value is `RSKImageCropModeCircle`.
  104. */
  105. @property (assign, nonatomic) RSKImageCropMode cropMode;
  106. /**
  107. The crop rectangle.
  108. @discussion The value is calculated at run time.
  109. */
  110. @property (readonly, nonatomic) CGRect cropRect;
  111. /**
  112. A value that specifies the current rotation angle of the image in radians.
  113. @discussion The value is calculated at run time.
  114. */
  115. @property (readonly, nonatomic) CGFloat rotationAngle;
  116. /**
  117. A floating-point value that specifies the current scale factor applied to the image.
  118. @discussion The value is calculated at run time.
  119. */
  120. @property (readonly, nonatomic) CGFloat zoomScale;
  121. /**
  122. A Boolean value that determines whether the image will always fill the mask space. Default value is `NO`.
  123. */
  124. @property (assign, nonatomic) BOOL avoidEmptySpaceAroundImage;
  125. /**
  126. A Boolean value that determines whether the image will always bounce horizontally. Default value is `NO`.
  127. */
  128. @property (assign, nonatomic) BOOL alwaysBounceHorizontal;
  129. /**
  130. A Boolean value that determines whether the image will always bounce vertically. Default value is `NO`.
  131. */
  132. @property (assign, nonatomic) BOOL alwaysBounceVertical;
  133. /**
  134. A Boolean value that determines whether the mask applies to the image after cropping. Default value is `NO`.
  135. */
  136. @property (assign, nonatomic) BOOL applyMaskToCroppedImage;
  137. /**
  138. A Boolean value that controls whether the rotaion gesture is enabled. Default value is `NO`.
  139. @discussion To support the rotation when `cropMode` is `RSKImageCropModeCustom` you must implement the data source method `imageCropViewControllerCustomMovementRect:`.
  140. */
  141. @property (assign, getter=isRotationEnabled, nonatomic) BOOL rotationEnabled;
  142. /// -------------------------------
  143. /// @name Accessing the UI Elements
  144. /// -------------------------------
  145. /**
  146. The Title Label.
  147. */
  148. @property (strong, nonatomic, readonly) UILabel *moveAndScaleLabel;
  149. /**
  150. The Cancel Button.
  151. */
  152. @property (strong, nonatomic, readonly) UIButton *cancelButton;
  153. /**
  154. The Choose Button.
  155. */
  156. @property (strong, nonatomic, readonly) UIButton *chooseButton;
  157. /// -------------------------------------------
  158. /// @name Checking of the Interface Orientation
  159. /// -------------------------------------------
  160. /**
  161. Returns a Boolean value indicating whether the user interface is currently presented in a portrait orientation.
  162. @return YES if the interface orientation is portrait, otherwise returns NO.
  163. */
  164. - (BOOL)isPortraitInterfaceOrientation;
  165. /// -------------------------------------
  166. /// @name Accessing the Layout Attributes
  167. /// -------------------------------------
  168. /**
  169. The inset of the circle mask rect's area within the crop view's area in portrait orientation. Default value is `15.0f`.
  170. */
  171. @property (assign, nonatomic) CGFloat portraitCircleMaskRectInnerEdgeInset;
  172. /**
  173. The inset of the square mask rect's area within the crop view's area in portrait orientation. Default value is `20.0f`.
  174. */
  175. @property (assign, nonatomic) CGFloat portraitSquareMaskRectInnerEdgeInset;
  176. /**
  177. 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`.
  178. */
  179. @property (assign, nonatomic) CGFloat portraitMoveAndScaleLabelTopAndCropViewTopVerticalSpace;
  180. /**
  181. 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`.
  182. */
  183. @property (assign, nonatomic) CGFloat portraitCropViewBottomAndCancelButtonBottomVerticalSpace;
  184. /**
  185. 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`.
  186. */
  187. @property (assign, nonatomic) CGFloat portraitCropViewBottomAndChooseButtonBottomVerticalSpace;
  188. /**
  189. 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`.
  190. */
  191. @property (assign, nonatomic) CGFloat portraitCancelButtonLeadingAndCropViewLeadingHorizontalSpace;
  192. /**
  193. 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`.
  194. */
  195. @property (assign, nonatomic) CGFloat portraitCropViewTrailingAndChooseButtonTrailingHorizontalSpace;
  196. /**
  197. The inset of the circle mask rect's area within the crop view's area in landscape orientation. Default value is `45.0f`.
  198. */
  199. @property (assign, nonatomic) CGFloat landscapeCircleMaskRectInnerEdgeInset;
  200. /**
  201. The inset of the square mask rect's area within the crop view's area in landscape orientation. Default value is `45.0f`.
  202. */
  203. @property (assign, nonatomic) CGFloat landscapeSquareMaskRectInnerEdgeInset;
  204. /**
  205. 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`.
  206. */
  207. @property (assign, nonatomic) CGFloat landscapeMoveAndScaleLabelTopAndCropViewTopVerticalSpace;
  208. /**
  209. 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`.
  210. */
  211. @property (assign, nonatomic) CGFloat landscapeCropViewBottomAndCancelButtonBottomVerticalSpace;
  212. /**
  213. 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`.
  214. */
  215. @property (assign, nonatomic) CGFloat landscapeCropViewBottomAndChooseButtonBottomVerticalSpace;
  216. /**
  217. 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`.
  218. */
  219. @property (assign, nonatomic) CGFloat landscapeCancelButtonLeadingAndCropViewLeadingHorizontalSpace;
  220. /**
  221. 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`.
  222. */
  223. @property (assign, nonatomic) CGFloat landscapeCropViewTrailingAndChooseButtonTrailingHorizontalSpace;
  224. @end
  225. /**
  226. 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.
  227. */
  228. @protocol RSKImageCropViewControllerDataSource <NSObject>
  229. /**
  230. Asks the data source a custom rect for the mask.
  231. @param controller The crop view controller object to whom a rect is provided.
  232. @return A custom rect for the mask.
  233. */
  234. - (CGRect)imageCropViewControllerCustomMaskRect:(RSKImageCropViewController *)controller;
  235. /**
  236. Asks the data source a custom path for the mask.
  237. @param controller The crop view controller object to whom a path is provided.
  238. @return A custom path for the mask.
  239. */
  240. - (UIBezierPath *)imageCropViewControllerCustomMaskPath:(RSKImageCropViewController *)controller;
  241. /**
  242. Asks the data source a custom rect in which the image can be moved.
  243. @param controller The crop view controller object to whom a rect is provided.
  244. @return A custom rect in which the image can be moved.
  245. */
  246. - (CGRect)imageCropViewControllerCustomMovementRect:(RSKImageCropViewController *)controller;
  247. @end
  248. /**
  249. The `RSKImageCropViewControllerDelegate` protocol defines messages sent to a image crop view controller delegate when crop image was canceled or the original image was cropped.
  250. */
  251. @protocol RSKImageCropViewControllerDelegate <NSObject>
  252. /**
  253. Tells the delegate that crop image has been canceled.
  254. */
  255. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller;
  256. /**
  257. Tells the delegate that the original image has been cropped. Additionally provides a crop rect and a rotation angle used to produce image.
  258. */
  259. - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle;
  260. @optional
  261. /**
  262. Tells the delegate that the image has been displayed.
  263. */
  264. - (void)imageCropViewControllerDidDisplayImage:(RSKImageCropViewController *)controller;
  265. /**
  266. Tells the delegate that the original image will be cropped.
  267. */
  268. - (void)imageCropViewController:(RSKImageCropViewController *)controller willCropImage:(UIImage *)originalImage;
  269. @end
  270. NS_ASSUME_NONNULL_END