LYLPhotoTailoringViewController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #define UIColorFromRGBValue(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
  2. #define Font_17 [UIFont systemFontOfSize:17]
  3. #define Color_ffffff UIColorFromRGBValue(0Xffffff)
  4. #import "LYLPhotoTailoringViewController.h"
  5. #import "PhotoMaskView.h"
  6. #import "UIImage+Crop.h"
  7. #import "UINavigationController+FDFullscreenPopGesture.h"
  8. @interface LYLPhotoTailoringViewController ()<UIScrollViewDelegate,PhotoMaskViewDelegate>
  9. {
  10. CGRect _rect;
  11. UIImageView *_imageView;
  12. UIView *_cropView;
  13. UIEdgeInsets _imageInset;
  14. CALayer *_layer;
  15. CGFloat _rotate;
  16. }
  17. @property (nonatomic,strong) PhotoMaskView *maskView;
  18. @property (nonatomic,strong) UIScrollView *scrollView;
  19. @property (nonatomic,strong) UIButton *btn;
  20. @end
  21. @implementation LYLPhotoTailoringViewController
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. self.fd_prefersNavigationBarHidden = YES;
  26. self.automaticallyAdjustsScrollViewInsets = NO;
  27. _rotate = 0;
  28. self.view.backgroundColor = [UIColor blackColor];
  29. self.oldImage = [UIImage fitScreenWithImage:self.oldImage];
  30. self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  31. _imageView = [[UIImageView alloc] initWithImage:self.oldImage];
  32. _imageView.center = self.view.center;
  33. _scrollView.delegate = self;
  34. self.scrollView.contentSize = self.oldImage.size;
  35. _scrollView.showsHorizontalScrollIndicator = NO;
  36. _scrollView.showsVerticalScrollIndicator = NO;
  37. _scrollView.bounces = YES;
  38. [self.scrollView addSubview:_imageView];
  39. [self.view addSubview:self.scrollView];
  40. // maskView
  41. CGFloat height = 0;
  42. CGFloat width = 0;
  43. self.cropHeight?height = self.cropHeight:0;
  44. if ( self.mode == PhotoMaskViewModeCircle) {
  45. if (self.cropWidth) {
  46. height = self.cropWidth;
  47. self.cropHeight = self.cropWidth;
  48. width = height;
  49. }else{
  50. width = self.cropHeight;
  51. self.cropWidth = self.cropHeight;
  52. height = width;
  53. }
  54. }else{
  55. height = self.cropHeight;
  56. width = self.cropWidth;
  57. }
  58. _maskView = [[PhotoMaskView alloc] initWithFrame:self.view.bounds width:width height:height];
  59. _maskView.mode = self.mode;
  60. _maskView.userInteractionEnabled = NO;
  61. _isDark?_maskView.isDark = YES:0;
  62. _lineColor?_maskView.lineColor = _lineColor:0;
  63. [self.view addSubview:self.maskView];
  64. self.maskView.delegate = self;
  65. [self bottomView];
  66. }
  67. -(void)bottomView
  68. {
  69. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-kiphoneXBootomHeight-50, SCREEN_WIDTH, 45)];
  70. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
  71. view.backgroundColor = [UIColor clearColor];
  72. [bottomView addSubview:view];
  73. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, -0.5, SCREEN_WIDTH, 0.5)];
  74. lineView.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:0.2];
  75. [bottomView addSubview:lineView];
  76. [self.view addSubview:bottomView];
  77. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(1*(SCREEN_WIDTH - 80), 10, 70, 30)];
  78. [btn setTitle:@"完成" forState:UIControlStateNormal];
  79. btn.titleLabel.font = Font_17;
  80. [bottomView addSubview:btn];
  81. [btn setTitleColor:Color_ffffff forState:UIControlStateNormal];
  82. [btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
  83. UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  84. [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
  85. cancelBtn.frame = CGRectMake(10, 10, 70, 30);
  86. cancelBtn.titleLabel.font = Font_17;
  87. [cancelBtn setTitleColor:Color_ffffff forState:UIControlStateNormal];
  88. [bottomView addSubview:cancelBtn];
  89. [cancelBtn addTarget:self action:@selector(backBtn)forControlEvents:UIControlEventTouchUpInside];
  90. }
  91. -(void)backBtn
  92. {
  93. if (self.delegate && [self.delegate respondsToSelector:@selector(imageCropperDidCancel:)]) {
  94. [self.delegate imageCropperDidCancel:self];
  95. }
  96. }
  97. - (void)buttonClicked
  98. {
  99. if (self.delegate && [self.delegate respondsToSelector:@selector(imageCropper:didFinished:)]) {
  100. [self.delegate imageCropper:self didFinished:[self cropImage]];
  101. }
  102. }
  103. -(void)layoutScrollViewWithRect:(CGRect)rect
  104. {
  105. _rect = rect;
  106. CGFloat top = (self.oldImage.size.height-rect.size.height)/2;
  107. CGFloat left = (self.oldImage.size.width-rect.size.width)/2;
  108. CGFloat bottom = self.view.bounds.size.height-top-rect.size.height;
  109. CGFloat right = self.view.bounds.size.width-rect.size.width-left;
  110. self.scrollView.contentInset = UIEdgeInsetsMake(top, left, bottom, right);
  111. CGFloat maskCircleWidth = rect.size.width;
  112. CGSize imageSize = self.oldImage.size;
  113. //setp 2: setup contentSize:
  114. CGFloat minimunZoomScale = imageSize.width < imageSize.height ? maskCircleWidth / imageSize.width : maskCircleWidth / imageSize.height;
  115. CGFloat maximumZoomScale = 1.5;
  116. self.scrollView.minimumZoomScale = minimunZoomScale;
  117. self.scrollView.maximumZoomScale = maximumZoomScale;
  118. self.scrollView.zoomScale = self.scrollView.zoomScale < minimunZoomScale ? minimunZoomScale : self.scrollView.zoomScale;
  119. _imageInset = self.scrollView.contentInset;
  120. }
  121. - (UIImage *)cropImage
  122. {
  123. CGFloat zoomScale = _scrollView.zoomScale;
  124. CGFloat offsetX = _scrollView.contentOffset.x;
  125. CGFloat offsetY = _scrollView.contentOffset.y;
  126. CGFloat aX = offsetX>=0 ? offsetX+_imageInset.left : (_imageInset.left - ABS(offsetX));
  127. CGFloat aY = offsetY>=0 ? offsetY+_imageInset.top : (_imageInset.top - ABS(offsetY));
  128. aX = aX / zoomScale;
  129. aY = aY / zoomScale;
  130. CGFloat aWidth = MAX(self.cropWidth / zoomScale, self.cropWidth);
  131. CGFloat aHeight = MAX(self.cropHeight / zoomScale, self.cropHeight);
  132. if (zoomScale>1) {
  133. aWidth = self.cropWidth/zoomScale;
  134. aHeight = self.cropHeight/zoomScale;
  135. }
  136. UIImage *image = [self.oldImage cropImageWithX:aX y:aY width:aWidth height:aHeight];
  137. image = [UIImage imageWithImageSimple:image scaledToSize:CGSizeMake(self.cropWidth, self.cropHeight)];
  138. return image;
  139. }
  140. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
  141. {
  142. return _imageView;
  143. }
  144. -(void)viewWillAppear:(BOOL)animated
  145. {
  146. [super viewWillAppear:animated];
  147. self.navigationController.navigationBarHidden = YES;
  148. [UIApplication sharedApplication].statusBarHidden = YES;
  149. //禁止返回
  150. id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
  151. UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
  152. [self.view addGestureRecognizer:pan];
  153. }
  154. -(void)viewWillDisappear:(BOOL)animated
  155. {
  156. [super viewWillDisappear:animated];
  157. self.navigationController.navigationBarHidden = NO;
  158. [UIApplication sharedApplication].statusBarHidden = NO;
  159. }
  160. @end