dkahgld

STPhotoKitController.m 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // STPhotoKitController.m
  3. // STPhotoKit
  4. //
  5. // Created by 沈兆良 on 16/2/26.
  6. // Copyright © 2016年 沈兆良. All rights reserved.
  7. //
  8. #import "STPhotoKitController.h"
  9. #import "STConfig.h"
  10. NS_ASSUME_NONNULL_BEGIN
  11. @interface STPhotoKitController ()<UIGestureRecognizerDelegate>
  12. /** 1.图片 */
  13. @property (nonatomic, strong) UIImageView *imageView;
  14. /** 2.取消按钮 */
  15. @property (nonatomic, strong) UIButton *buttonCancel;
  16. /** 3.确认按钮 */
  17. @property (nonatomic, strong) UIButton *buttonConfirm;
  18. /** 4.覆盖图 */
  19. @property (nonatomic, strong) UIView *viewOverlay;
  20. /** 5.图片返回初始状态 */
  21. @property (nonatomic, strong) UIButton *buttonBack;
  22. /** 6.修剪的位置 */
  23. @property (nonatomic, assign) CGRect rectClip;
  24. @end
  25. NS_ASSUME_NONNULL_END
  26. @implementation STPhotoKitController
  27. #pragma mark - --- lift cycle 生命周期 ---
  28. - (instancetype)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. self.sizeClip = CGSizeMake(ScreenWidth, ScreenWidth);
  33. }
  34. return self;
  35. }
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. [self.viewOverlay setHollowWithCenterFrame:self.rectClip];
  39. [self.view setBackgroundColor:[UIColor blackColor]];
  40. [self.view addSubview:self.imageView];
  41. [self.view addSubview:self.viewOverlay];
  42. [self.view addSubview:self.buttonCancel];
  43. [self.view addSubview:self.buttonConfirm];
  44. [self.view addSubview:self.buttonBack];
  45. [self addGestureRecognizerToView:self.view];
  46. }
  47. #pragma mark - --- delegate 视图委托 ---
  48. #pragma mark - --- event response 事件相应 ---
  49. /**
  50. * 1.取消操作
  51. */
  52. - (void)cancel
  53. {
  54. [self dismissViewControllerAnimated:YES completion:nil];
  55. }
  56. /**
  57. * 2.确认操作
  58. */
  59. - (void)confirm
  60. {
  61. if ([self.delegate respondsToSelector:@selector(photoKitController:resultImage:)]) {
  62. [self.delegate photoKitController:self resultImage:[self getResultImage]];
  63. }
  64. [self dismissViewControllerAnimated:YES completion:nil];
  65. }
  66. /**
  67. * 3.返回原来位置
  68. */
  69. - (void)recover
  70. {
  71. [self.buttonBack setHidden:YES];
  72. [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  73. [self.imageView setTransform:CGAffineTransformMakeRotation(0)];
  74. [self.imageView setFrame:self.view.bounds];
  75. } completion:^(BOOL finished) {
  76. }];
  77. }
  78. /**
  79. * 4.处理旋转手势
  80. */
  81. - (void)rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
  82. {
  83. [self.buttonBack setHidden:NO];
  84. UIView *view = self.imageView;
  85. if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
  86. view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
  87. [rotationGestureRecognizer setRotation:0];
  88. }
  89. }
  90. /**
  91. * 5.处理缩放手势
  92. */
  93. - (void)pinchView:(UIPinchGestureRecognizer *)pinchGestureRecognizer
  94. {
  95. [self.buttonBack setHidden:NO];
  96. UIView *view = self.imageView;
  97. if (pinchGestureRecognizer.state == UIGestureRecognizerStateBegan || pinchGestureRecognizer.state == UIGestureRecognizerStateChanged) {
  98. view.transform = CGAffineTransformScale(view.transform, pinchGestureRecognizer.scale, pinchGestureRecognizer.scale);
  99. pinchGestureRecognizer.scale = 1;
  100. }
  101. }
  102. /**
  103. * 6.处理拖拉手势
  104. */
  105. - (void)panView:(UIPanGestureRecognizer *)panGestureRecognizer
  106. {
  107. [self.buttonBack setHidden:NO];
  108. UIView *view = self.imageView;
  109. if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
  110. CGPoint translation = [panGestureRecognizer translationInView:view.superview];
  111. [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
  112. [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
  113. }
  114. }
  115. #pragma mark - --- private methods 私有方法 ---
  116. /**
  117. * 1.添加所有的手势
  118. */
  119. - (void) addGestureRecognizerToView:(UIView *)view
  120. {
  121. // 旋转手势
  122. UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
  123. [view addGestureRecognizer:rotationGestureRecognizer];
  124. // 缩放手势
  125. UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];
  126. [view addGestureRecognizer:pinchGestureRecognizer];
  127. // 移动手势
  128. UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
  129. [view addGestureRecognizer:panGestureRecognizer];
  130. }
  131. /**
  132. * 2.获取指定的图片
  133. *
  134. * @return <#return value description#>
  135. */
  136. - (UIImage *)getResultImage
  137. {
  138. UIImage *image = [self.view imageFromSelfView];
  139. return [image croppedImage:self.rectClip];
  140. }
  141. #pragma mark - --- getters and setters 属性 ---
  142. - (void)setImageOriginal:(UIImage *)imageOriginal
  143. {
  144. _imageOriginal = imageOriginal;
  145. self.imageView.image = imageOriginal;
  146. }
  147. - (void)setSizeClip:(CGSize)sizeClip
  148. {
  149. _sizeClip = sizeClip;
  150. CGFloat clipW = sizeClip.width;
  151. CGFloat clipH = sizeClip.height;
  152. CGFloat clipX = (ScreenWidth - clipW)/2;
  153. CGFloat clipY = (ScreenHeight - clipH)/2;
  154. _rectClip = CGRectMake(clipX, clipY, clipW, clipH);
  155. }
  156. /** 1.图片 */
  157. - (UIImageView *)imageView
  158. {
  159. if (!_imageView) {
  160. _imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
  161. [_imageView setContentMode:UIViewContentModeScaleAspectFit];
  162. [_imageView setUserInteractionEnabled:YES];
  163. [_imageView setMultipleTouchEnabled:YES];
  164. }
  165. return _imageView;
  166. }
  167. /** 2.取消按钮 */
  168. - (UIButton *)buttonCancel
  169. {
  170. if (!_buttonCancel) {
  171. CGFloat buttonW = 40;
  172. CGFloat buttonH = 28;
  173. CGFloat buttonX = STMarginBig;
  174. CGFloat buttonY = ScreenHeight - buttonH - STMarginBig;
  175. _buttonCancel = [[UIButton alloc]initWithFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
  176. [_buttonCancel setBackgroundColor:RGBA(0, 0, 0, 50.0/255)];
  177. [_buttonCancel setTitle:@"取消" forState:UIControlStateNormal];
  178. [_buttonCancel setTitleColor:[UIColor whiteColor]
  179. forState:UIControlStateNormal];
  180. [_buttonCancel.titleLabel setFont:[UIFont systemFontOfSize:14]];
  181. [_buttonCancel setClipsToBounds:YES];
  182. [_buttonCancel.layer setCornerRadius:2];
  183. [_buttonCancel.layer setBorderWidth:0.5];
  184. [_buttonCancel.layer setBorderColor:RGBA(255, 255, 255, 60.0/255).CGColor];
  185. [_buttonCancel addTarget:self
  186. action:@selector(cancel)
  187. forControlEvents:UIControlEventTouchUpInside];
  188. }
  189. return _buttonCancel;
  190. }
  191. /** 3.确认按钮 */
  192. - (UIButton *)buttonConfirm
  193. {
  194. if (!_buttonConfirm) {
  195. CGFloat buttonW = 40;
  196. CGFloat buttonH = 28;
  197. CGFloat buttonX = ScreenWidth - buttonW - STMarginBig;
  198. CGFloat buttonY = ScreenHeight - buttonH - STMarginBig;
  199. _buttonConfirm = [[UIButton alloc]initWithFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
  200. [_buttonConfirm setBackgroundColor:RGBA(0, 0, 0, 50.0/255)];
  201. [_buttonConfirm setTitle:@"确定" forState:UIControlStateNormal];
  202. [_buttonConfirm setTitleColor:[UIColor whiteColor]
  203. forState:UIControlStateNormal];
  204. [_buttonConfirm.titleLabel setFont:[UIFont systemFontOfSize:14]];
  205. [_buttonConfirm setClipsToBounds:YES];
  206. [_buttonConfirm.layer setCornerRadius:2];
  207. [_buttonConfirm.layer setBorderWidth:0.5];
  208. [_buttonConfirm.layer setBorderColor:RGBA(255, 255, 255, 60.0/255).CGColor];
  209. [_buttonConfirm addTarget:self
  210. action:@selector(confirm)
  211. forControlEvents:UIControlEventTouchUpInside];
  212. }
  213. return _buttonConfirm;
  214. }
  215. /** 4.覆盖图 */
  216. - (UIView *)viewOverlay
  217. {
  218. if (!_viewOverlay) {
  219. _viewOverlay = [[UIView alloc]initWithFrame:self.view.bounds];
  220. [_viewOverlay setBackgroundColor:[UIColor blackColor]];
  221. [_viewOverlay setAlpha:180.0/255];
  222. // [_viewOverlay addSubview:self.viewRatio];
  223. }
  224. return _viewOverlay;
  225. }
  226. /** 5.图片返回初始状态 */
  227. - (UIButton *)buttonBack
  228. {
  229. if (!_buttonBack) {
  230. CGFloat buttonW = 40;
  231. CGFloat buttonH = 28;
  232. CGFloat buttonX = ScreenWidth - buttonW - STMarginBig;
  233. CGFloat buttonY = ScreenHeight - buttonH - STMarginBig;
  234. _buttonBack = [[UIButton alloc]initWithFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH)];
  235. [_buttonBack setCenterX:ScreenWidth/2];
  236. [_buttonBack setBackgroundColor:RGBA(0, 0, 0, 50.0/255)];
  237. [_buttonBack setTitle:@"复原" forState:UIControlStateNormal];
  238. [_buttonBack setTitleColor:[UIColor whiteColor]
  239. forState:UIControlStateNormal];
  240. [_buttonBack.titleLabel setFont:[UIFont systemFontOfSize:14]];
  241. [_buttonBack setClipsToBounds:YES];
  242. [_buttonBack.layer setCornerRadius:2];
  243. [_buttonBack.layer setBorderWidth:0.5];
  244. [_buttonBack.layer setBorderColor:RGBA(255, 255, 255, 60.0/255).CGColor];
  245. [_buttonBack setHidden:YES];
  246. [_buttonBack addTarget:self
  247. action:@selector(recover)
  248. forControlEvents:UIControlEventTouchUpInside];
  249. }
  250. return _buttonBack;
  251. }
  252. @end