HXPhotoEditSplashMaskLayer.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // HXPhotoEditSplashMaskLayer.m
  3. // photoEditDemo
  4. //
  5. // Created by Silence on 2020/7/1.
  6. // Copyright © 2020 Silence. All rights reserved.
  7. //
  8. #import "HXPhotoEditSplashMaskLayer.h"
  9. #import "UIImage+HXExtension.h"
  10. #define HXRadiansToDegrees(x) (180.0 * x / M_PI)
  11. CGFloat angleBetweenPoints(CGPoint startPoint, CGPoint endPoint) {
  12. CGPoint Xpoint = CGPointMake(startPoint.x + 100, startPoint.y);
  13. CGFloat a = endPoint.x - startPoint.x;
  14. CGFloat b = endPoint.y - startPoint.y;
  15. CGFloat c = Xpoint.x - startPoint.x;
  16. CGFloat d = Xpoint.y - startPoint.y;
  17. CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
  18. if (startPoint.y>endPoint.y) {
  19. rads = -rads;
  20. }
  21. return rads;
  22. }
  23. CGFloat angleBetweenLines(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {
  24. CGFloat a = line1End.x - line1Start.x;
  25. CGFloat b = line1End.y - line1Start.y;
  26. CGFloat c = line2End.x - line2Start.x;
  27. CGFloat d = line2End.y - line2Start.y;
  28. CGFloat rads = acos(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
  29. return HXRadiansToDegrees(rads);
  30. }
  31. @implementation HXPhotoEditSplashBlur
  32. @end
  33. @implementation HXPhotoEditSplashImageBlur
  34. @end
  35. @interface HXPhotoEditSplashMaskLayer ()
  36. @end
  37. @implementation HXPhotoEditSplashMaskLayer
  38. - (instancetype)init {
  39. self = [super init];
  40. if (self) {
  41. // self.contentsScale = [[UIScreen mainScreen] scale];
  42. self.backgroundColor = [UIColor clearColor].CGColor;
  43. _lineArray = [@[] mutableCopy];
  44. }
  45. return self;
  46. }
  47. - (void)drawInContext:(CGContextRef)context {
  48. UIGraphicsPushContext( context );
  49. [[UIColor clearColor] setFill];
  50. UIRectFill(self.bounds);
  51. CGContextSetLineCap(context, kCGLineCapRound);
  52. CGContextSetLineJoin(context, kCGLineJoinRound);
  53. for (NSInteger i = 0; i < self.lineArray.count; i++) {
  54. HXPhotoEditSplashBlur *blur = self.lineArray[i];
  55. CGRect rect = blur.rect;
  56. CGContextSetStrokeColorWithColor(context, (blur.color ? blur.color.CGColor : [UIColor clearColor].CGColor));
  57. // 模糊矩形可以用到,用于填充矩形
  58. CGContextSetFillColorWithColor(context, (blur.color ? blur.color.CGColor : [UIColor clearColor].CGColor));
  59. if ([blur isMemberOfClass:[HXPhotoEditSplashImageBlur class]]) {
  60. UIImage *image = [UIImage hx_imageNamed:((HXPhotoEditSplashImageBlur *)blur).imageName];
  61. if (image) {
  62. // CGPoint firstPoint = CGPointZero;
  63. // if (i > 0) {
  64. // HXPhotoEditSplashBlur *prevBlur = self.lineArray[i-1];
  65. // firstPoint = prevBlur.rect.origin;
  66. // }
  67. /** 创建颜色图片 */
  68. CGColorSpaceRef colorRef = CGColorSpaceCreateDeviceRGB();
  69. CGContextRef contextRef = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, image.size.width*4, colorRef, kCGImageAlphaPremultipliedFirst);
  70. CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
  71. CGContextClipToMask(contextRef, imageRect, image.CGImage);
  72. CGContextSetFillColorWithColor(contextRef, (blur.color ? blur.color.CGColor : [UIColor clearColor].CGColor));
  73. CGContextFillRect(contextRef,imageRect);
  74. /** 生成图片 */
  75. CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
  76. CGContextDrawImage(context, rect, imageRef);
  77. CGImageRelease(imageRef);
  78. CGContextRelease(contextRef);
  79. CGColorSpaceRelease(colorRef);
  80. }
  81. } else {
  82. // 模糊矩形 填充 画完一个小正方形
  83. CGContextFillRect(context, rect);
  84. }
  85. }
  86. UIGraphicsPopContext();
  87. }
  88. @end