PhotoMaskView.m 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #import "PhotoMaskView.h"
  2. @implementation PhotoMaskView
  3. {
  4. CGRect _squareRect; // 外切正方形
  5. CGFloat _cropWidth;
  6. CGFloat _cropHeight;
  7. }
  8. -(instancetype)initWithFrame:(CGRect)frame width:(CGFloat)cropWidth height:(CGFloat)height
  9. {
  10. self = [super initWithFrame:frame];
  11. if (self) {
  12. self.mode = PhotoMaskViewModeCircle;
  13. self.backgroundColor = [UIColor clearColor];
  14. _cropWidth = cropWidth;
  15. _cropHeight = height;
  16. self.isDark = NO;
  17. self.lineColor = [UIColor whiteColor];
  18. }
  19. return self;
  20. }
  21. -(void)drawRect:(CGRect)rect
  22. {
  23. [super drawRect:rect];
  24. if (self.mode == PhotoMaskViewModeCircle) {
  25. [self crop:rect];
  26. }else{
  27. [self crop2:rect];
  28. }
  29. if (self.delegate && [self.delegate respondsToSelector:@selector(layoutScrollViewWithRect:)]) {
  30. [self.delegate layoutScrollViewWithRect:_squareRect];
  31. }
  32. }
  33. -(void)crop2:(CGRect)rect
  34. {
  35. CGFloat width = rect.size.width;
  36. CGFloat height = rect.size.height;
  37. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  38. CGContextSaveGState(contextRef);
  39. CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.4);
  40. _squareRect = CGRectMake((width - _cropWidth) / 2, (height - _cropHeight) / 2, _cropWidth, _cropHeight);
  41. UIBezierPath *squarePath = [UIBezierPath bezierPathWithRect:_squareRect];
  42. UIBezierPath *maskBezierPath = [UIBezierPath bezierPathWithRect:rect];
  43. [maskBezierPath appendPath:squarePath];
  44. maskBezierPath.usesEvenOddFillRule = YES;
  45. [maskBezierPath fill];
  46. CGContextSetLineWidth(contextRef, 2);
  47. if (self.isDark) {
  48. CGFloat length[2] = {5,5};
  49. CGContextSetLineDash(contextRef, 0, length, 2);
  50. }
  51. CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);
  52. [squarePath stroke];
  53. CGContextRestoreGState(contextRef);
  54. self.layer.contentsGravity = kCAGravityCenter;
  55. }
  56. -(void)crop:(CGRect)rect
  57. {
  58. CGFloat width = rect.size.width;
  59. CGFloat height = rect.size.height;
  60. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  61. CGContextSaveGState(contextRef);
  62. CGContextSetRGBFillColor(contextRef, 0, 0, 0, 0.4);
  63. _squareRect = CGRectMake((width - _cropWidth) / 2, (height - _cropWidth) / 2, _cropWidth, _cropWidth);
  64. UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:_squareRect];
  65. UIBezierPath *maskBezierPath = [UIBezierPath bezierPathWithRect:rect];
  66. [maskBezierPath appendPath:circlePath];
  67. maskBezierPath.usesEvenOddFillRule = YES;
  68. [maskBezierPath fill];
  69. CGContextSetLineWidth(contextRef, 2);
  70. if (self.isDark) {
  71. CGFloat length[2] = {5,5};
  72. CGContextSetLineDash(contextRef, 0, length, 2);
  73. }
  74. CGContextSetStrokeColorWithColor(contextRef, _lineColor.CGColor);
  75. [circlePath stroke];
  76. CGContextRestoreGState(contextRef);
  77. self.layer.contentsGravity = kCAGravityCenter;
  78. }
  79. @end