ZBCornerRadiusTool.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // ZBCornerRadiusTool.m
  3. // ZBCornerDemo
  4. //
  5. // Created by Zuobian on 2019/12/24.
  6. // Copyright © 2019 admin. All rights reserved.
  7. //
  8. #import "ZBCornerRadiusTool.h"
  9. #import "BezierPathTool.h"
  10. @implementation ZBCornerRadiusTool
  11. /// 绘制裁剪圆角后图片
  12. /// @param radius 圆角
  13. /// @param rectSize 视图尺寸
  14. /// @param fillColor 填充色
  15. /// @param cornerStyle 圆角位置
  16. + (UIImage *)drawAntiRoundedCornerImageWithRadius:(float)radius rectSize:(CGSize)rectSize fillColor:(UIColor *)fillColor cornerStyle:(UIRectCorner)cornerStyle {
  17. UIGraphicsBeginImageContextWithOptions(rectSize, false, [UIScreen mainScreen].scale);
  18. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  19. UIBezierPath *bezierPath = [UIBezierPath bezierPath];
  20. [ZBCornerRadiusTool configPathWithBezierPath:&bezierPath Radius:radius rectSize:rectSize cornerStyle:cornerStyle fillColor:fillColor];
  21. CGContextDrawPath(contextRef, kCGPathFillStroke);
  22. UIImage *antiRoundedCornerImage = UIGraphicsGetImageFromCurrentImageContext();
  23. UIGraphicsEndImageContext();
  24. return antiRoundedCornerImage;
  25. }
  26. /// 绘制裁剪圆角后图片
  27. /// @param radius_TL 左上角半径
  28. /// @param radius_TR 右上角半径
  29. /// @param radius_BL 左下角半径
  30. /// @param radius_BR 右下角半径
  31. /// @param rectSize 视图尺寸
  32. /// @param fillColor 填充色
  33. + (UIImage *)drawAntiRoundedCornerWithRadius_TL:(float)radius_TL
  34. radius_TR:(float)radius_TR
  35. radius_BL:(float)radius_BL
  36. radius_BR:(float)radius_BR
  37. rectSize:(CGSize)rectSize
  38. fillColor:(UIColor *)fillColor {
  39. UIGraphicsBeginImageContextWithOptions(rectSize, false, [UIScreen mainScreen].scale);
  40. CGContextRef contextRef = UIGraphicsGetCurrentContext();
  41. UIBezierPath *bezierPath = [UIBezierPath bezierPath];
  42. [ZBCornerRadiusTool configPathWithBezierPath:&bezierPath radius_TL:radius_TL radius_TR:radius_TR radius_BL:radius_BL radius_BR:radius_BR rectSize:rectSize fillColor:fillColor];
  43. CGContextDrawPath(contextRef, kCGPathFillStroke);
  44. UIImage *antiRoundedCornerImage = UIGraphicsGetImageFromCurrentImageContext();
  45. UIGraphicsEndImageContext();
  46. return antiRoundedCornerImage;
  47. }
  48. // 对任意一个或多个角设置圆角
  49. + (void)configPathWithBezierPath:(UIBezierPath **)bezier Radius:(float)radius rectSize:(CGSize)rectSize cornerStyle:(UIRectCorner)cornerStyle fillColor:(UIColor *)fillColor {
  50. UIBezierPath *bezierPath = *bezier;
  51. BezierPathTool *tool = [[BezierPathTool alloc]initWithRadius:radius rectSize:rectSize fillColor:fillColor];
  52. if (cornerStyle == UIRectCornerAllCorners) {
  53. [tool configAllCornerPoint];
  54. }else {
  55. if (cornerStyle == UIRectCornerTopLeft) {
  56. [tool configTopLeftPoint];
  57. }
  58. if (cornerStyle == UIRectCornerTopRight) {
  59. [tool configTopRightPoint];
  60. }
  61. if (cornerStyle == UIRectCornerBottomLeft) {
  62. [tool configBottomLeftPoint];
  63. }
  64. if (cornerStyle == UIRectCornerBottomRight) {
  65. [tool configBottomRightPoint];
  66. }
  67. }
  68. bezierPath = [tool configCornerBezierPath:bezierPath];
  69. NSLog(@"bezierPath内存地址 -- %p",bezierPath);
  70. }
  71. // 对四个角分别设置圆角,0即为不进行圆角设置
  72. + (void)configPathWithBezierPath:(UIBezierPath **)bezier
  73. radius_TL:(float)radius_TL
  74. radius_TR:(float)radius_TR
  75. radius_BL:(float)radius_BL
  76. radius_BR:(float)radius_BR
  77. rectSize:(CGSize)rectSize
  78. fillColor:(UIColor *)fillColor {
  79. UIBezierPath *bezierPath = *bezier;
  80. BezierPathTool *tool = [[BezierPathTool alloc]initWithRadius_TopLeft:radius_TL radius_TopRight:radius_TR radius_BottomLeft:radius_BL radius_BottomRight:radius_BR rectSize:rectSize fillColor:fillColor];
  81. bezierPath = [tool configCornerBezierPath:bezierPath];
  82. NSLog(@"bezierPath内存地址 -- %p",bezierPath);
  83. }
  84. @end