KKMosaicView.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KKMasaicView.m
  3. // WWImageEdit
  4. //
  5. // Created by 邬维 on 2017/1/11.
  6. // Copyright © 2017年 kook. All rights reserved.
  7. //
  8. #import "KKMosaicView.h"
  9. @interface KKMosaicView ()
  10. @property (nonatomic, strong) UIImageView *surfaceImageView;
  11. @property (nonatomic, strong) CALayer *imageLayer;
  12. @property (nonatomic, strong) CAShapeLayer *shapeLayer;
  13. @property (nonatomic, assign) CGMutablePathRef path;
  14. @end
  15. @implementation KKMosaicView
  16. - (void)dealloc
  17. {
  18. if (self.path) {
  19. CGPathRelease(_path);
  20. }
  21. }
  22. - (instancetype)initWithFrame:(CGRect)frame
  23. {
  24. self = [super initWithFrame:frame];
  25. if (self) {
  26. //添加imageview(surfaceImageView)到self上
  27. self.surfaceImageView = [[UIImageView alloc]initWithFrame:self.bounds];
  28. [self addSubview:self.surfaceImageView];
  29. //添加layer(imageLayer)到self上
  30. self.imageLayer = [CALayer layer];
  31. self.imageLayer.frame = self.bounds;
  32. [self.layer addSublayer:self.imageLayer];
  33. self.shapeLayer = [CAShapeLayer layer];
  34. self.shapeLayer.frame = self.bounds;
  35. self.shapeLayer.lineCap = kCALineCapRound;
  36. self.shapeLayer.lineJoin = kCALineJoinRound;
  37. //手指移动时 画笔的宽度
  38. self.shapeLayer.lineWidth = 20.f;
  39. self.shapeLayer.strokeColor = [UIColor blueColor].CGColor;
  40. self.shapeLayer.fillColor = nil;
  41. [self.layer addSublayer:self.shapeLayer];
  42. self.imageLayer.mask = self.shapeLayer;
  43. CGMutablePathRef pathRef = CGPathCreateMutable();
  44. self.path = CGPathCreateMutableCopy(pathRef);
  45. CGPathRelease(pathRef);
  46. }
  47. return self;
  48. }
  49. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  50. {
  51. [super touchesBegan:touches withEvent:event];
  52. UITouch *touch = [touches anyObject];
  53. CGPoint point = [touch locationInView:self];
  54. CGPathMoveToPoint(self.path, NULL, point.x, point.y);
  55. CGMutablePathRef path = CGPathCreateMutableCopy(self.path);
  56. self.shapeLayer.path = path;
  57. CGPathRelease(path);
  58. }
  59. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  60. {
  61. [super touchesMoved:touches withEvent:event];
  62. UITouch *touch = [touches anyObject];
  63. CGPoint point = [touch locationInView:self];
  64. CGPathAddLineToPoint(self.path, NULL, point.x, point.y);
  65. CGMutablePathRef path = CGPathCreateMutableCopy(self.path);
  66. //
  67. CGContextRef currentContext = UIGraphicsGetCurrentContext();
  68. if (!currentContext) {
  69. UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 0);
  70. }
  71. CGContextAddPath(currentContext, path);
  72. [[UIColor blueColor] setStroke];
  73. CGContextDrawPath(currentContext, kCGPathStroke);
  74. self.shapeLayer.path = path;
  75. CGPathRelease(path);
  76. }
  77. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  78. {
  79. [super touchesEnded:touches withEvent:event];
  80. }
  81. - (void)setImage:(UIImage *)image
  82. {
  83. //底图
  84. _image = image;
  85. self.imageLayer.contents = (id)image.CGImage;
  86. }
  87. - (void)setSurfaceImage:(UIImage *)surfaceImage
  88. {
  89. //顶图
  90. _surfaceImage = surfaceImage;
  91. self.surfaceImageView.image = surfaceImage;
  92. }
  93. @end