猎豆优选

CropImageController.m 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // CropImageController.m
  3. // CropImage
  4. //
  5. // Created by limuyun on 2017/1/10.
  6. // Copyright © 2017年 biiway. All rights reserved.
  7. //
  8. #import "CropImageController.h"
  9. #import "UIImage+Crop.h"
  10. #define ScreenWidth [UIScreen mainScreen].bounds.size.width
  11. #define ScreenHeight [UIScreen mainScreen].bounds.size.height
  12. @interface CropImageController ()<UIScrollViewDelegate>
  13. @property (nonatomic, strong) UIScrollView * scrollView;
  14. @property (nonatomic, strong) UIImageView * imageView;
  15. @property (nonatomic, strong) UIImage * originalImage;
  16. @end
  17. @implementation CropImageController
  18. - (instancetype)initWithImage:(UIImage *)originalImage delegate:(id)delegate {
  19. self = [super init];
  20. if (self) {
  21. self.view.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  22. _delegate = delegate;
  23. _originalImage = originalImage;
  24. }
  25. return self;
  26. }
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view.
  30. self.view.backgroundColor = [UIColor blackColor];
  31. self.automaticallyAdjustsScrollViewInsets = NO;
  32. CGFloat height = (ScreenHeight - ScreenWidth)/2.0;
  33. _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, height ,ScreenWidth,ScreenWidth)];
  34. _scrollView.bouncesZoom = YES;
  35. _scrollView.minimumZoomScale = 1;
  36. _scrollView.maximumZoomScale = 3;
  37. _scrollView.zoomScale = 1;
  38. _scrollView.delegate = self;
  39. _scrollView.layer.masksToBounds = NO;
  40. _scrollView.showsHorizontalScrollIndicator = NO;
  41. _scrollView.showsVerticalScrollIndicator = NO;
  42. _scrollView.layer.borderWidth = 1.5;
  43. _scrollView.layer.borderColor = [UIColor whiteColor].CGColor;
  44. if (_ovalClip) {
  45. _scrollView.layer.cornerRadius = ScreenWidth/2.0;
  46. }
  47. self.view.layer.masksToBounds = YES;
  48. if (_originalImage) {
  49. _imageView = [[UIImageView alloc] initWithImage:_originalImage];
  50. CGFloat img_width = ScreenWidth;
  51. CGFloat img_height = _originalImage.size.height * (img_width/_originalImage.size.width);
  52. CGFloat img_y= (img_height - self.view.bounds.size.width)/2.0;
  53. _imageView.frame = CGRectMake(0,0, img_width, img_height);
  54. _imageView.userInteractionEnabled = YES;
  55. [_scrollView addSubview:_imageView];
  56. _scrollView.contentSize = CGSizeMake(img_width, img_height);
  57. _scrollView.contentOffset = CGPointMake(0, img_y);
  58. [self.view addSubview:_scrollView];
  59. }
  60. [self userInterface];
  61. }
  62. - (void)viewWillAppear:(BOOL)animated {
  63. [super viewWillAppear:animated];
  64. [self.navigationController setNavigationBarHidden:YES];
  65. }
  66. - (void)viewWillDisappear:(BOOL)animated {
  67. [super viewWillDisappear:animated];
  68. [self.navigationController setNavigationBarHidden:YES];
  69. }
  70. - (void)userInterface {
  71. CGRect cropframe = _scrollView.frame;
  72. UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds cornerRadius:0];
  73. UIBezierPath * cropPath = [UIBezierPath bezierPathWithRoundedRect:cropframe cornerRadius:0];
  74. if (_ovalClip) {
  75. cropPath = [UIBezierPath bezierPathWithOvalInRect:cropframe];
  76. }
  77. [path appendPath:cropPath];
  78. CAShapeLayer * layer = [[CAShapeLayer alloc] init];
  79. layer.fillColor = [UIColor colorWithRed:.0 green:.0 blue:.0 alpha:0.5].CGColor;
  80. layer.fillRule=kCAFillRuleEvenOdd;
  81. layer.path = path.CGPath;
  82. [self.view.layer addSublayer:layer];
  83. UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 46, self.view.bounds.size.width, 46)];
  84. view.backgroundColor = [UIColor colorWithRed:30/255.0 green:30/255.0 blue:30/255.0 alpha:0.7];
  85. view.userInteractionEnabled=YES;
  86. [self.view addSubview:view];
  87. UIButton * canncelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  88. canncelBtn.frame = CGRectMake(40, 0, 60, 44);
  89. canncelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  90. [canncelBtn setTitle:@"取 消" forState:UIControlStateNormal];
  91. [canncelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  92. [canncelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  93. [view addSubview:canncelBtn];
  94. UIButton * doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  95. doneBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, 0, 60, 44);
  96. doneBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  97. [doneBtn setTitle:@"完 成" forState:UIControlStateNormal];
  98. [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  99. [doneBtn addTarget:self action:@selector(doneBtnClick) forControlEvents:UIControlEventTouchUpInside];
  100. [view addSubview:doneBtn];
  101. }
  102. #pragma mark -- UIScrollViewDelegate
  103. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  104. return self.imageView;
  105. }
  106. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  107. //调整位置
  108. [self centerContent];
  109. }
  110. - (void)centerContent {
  111. CGRect imageViewFrame = _imageView.frame;
  112. CGRect scrollBounds = CGRectMake(0, 0, ScreenWidth, ScreenWidth);
  113. if (imageViewFrame.size.height > scrollBounds.size.height) {
  114. imageViewFrame.origin.y = 0.0f;
  115. }else {
  116. imageViewFrame.origin.y = (scrollBounds.size.height - imageViewFrame.size.height) / 2.0;
  117. }
  118. if (imageViewFrame.size.width < scrollBounds.size.width) {
  119. imageViewFrame.origin.x = (scrollBounds.size.width - imageViewFrame.size.width) /2.0;
  120. }else {
  121. imageViewFrame.origin.x = 0.0f;
  122. }
  123. _imageView.frame = imageViewFrame;
  124. }
  125. - (void)cancelBtnClick{
  126. [self.navigationController dismissViewControllerAnimated:YES completion:nil];
  127. [self.navigationController popViewControllerAnimated:YES];
  128. }
  129. - (UIImage *)cropImage {
  130. CGPoint offset = _scrollView.contentOffset;
  131. //图片缩放比例
  132. CGFloat zoom = _imageView.frame.size.width/_originalImage.size.width;
  133. //视网膜屏幕倍数相关
  134. zoom = zoom / [UIScreen mainScreen].scale;
  135. CGFloat width = _scrollView.frame.size.width;
  136. CGFloat height = _scrollView.frame.size.height;
  137. if (_imageView.frame.size.height < _scrollView.frame.size.height) {//太胖了,取中间部分
  138. offset = CGPointMake(offset.x + (width - _imageView.frame.size.height)/2.0, 0);
  139. width = height = _imageView.frame.size.height;
  140. }
  141. CGRect rec = CGRectMake(offset.x/zoom, offset.y/zoom,width/zoom,height/zoom);
  142. CGImageRef imageRef =CGImageCreateWithImageInRect([_originalImage CGImage],rec);
  143. UIImage * image = [[UIImage alloc]initWithCGImage:imageRef];
  144. CGImageRelease(imageRef);
  145. if (_ovalClip) {
  146. image = [image ovalClip];
  147. }
  148. return image;
  149. }
  150. - (void)doneBtnClick{
  151. if (_delegate && [_delegate respondsToSelector:@selector(cropImageDidFinishedWithImage:)]) {
  152. [_delegate cropImageDidFinishedWithImage:[self cropImage]];
  153. }
  154. [self.navigationController popViewControllerAnimated:YES];
  155. }
  156. #pragma mark - UIStatusBarStyle
  157. - (UIStatusBarStyle)preferredStatusBarStyle {
  158. return UIStatusBarStyleLightContent;
  159. }
  160. - (BOOL)prefersStatusBarHidden {
  161. return NO;
  162. }
  163. @end