UIImage+RKCircleImg.m 890 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // UIImage+RKCircleImg.m
  3. // iphoneLive
  4. //
  5. // Created by YB007 on 2019/12/1.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "UIImage+RKCircleImg.h"
  9. @implementation UIImage (RKCircleImg)
  10. - (instancetype)rk_circleImage {
  11. //开启图形上下文
  12. UIGraphicsBeginImageContext(self.size);
  13. //上下文
  14. CGContextRef ctx = UIGraphicsGetCurrentContext();
  15. //添加一个圆
  16. CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
  17. CGContextAddEllipseInRect(ctx, rect);
  18. //裁剪
  19. CGContextClip(ctx);
  20. //绘制图片
  21. [self drawInRect:rect];
  22. //获得图片
  23. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  24. //关闭图形上下文
  25. UIGraphicsEndImageContext();
  26. return image;
  27. }
  28. + (instancetype)rk_circleImageWith:(NSString *)name {
  29. return [[self imageNamed:name] rk_circleImage];
  30. }
  31. @end