UIImage+ERCategory.m 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // UIImage+ERCategory.m
  3. // ErHuDemo
  4. //
  5. // Created by 胡广宇 on 2017/7/11.
  6. // Copyright © 2017年 胡广宇. All rights reserved.
  7. //
  8. #import "UIImage+ERCategory.h"
  9. @implementation UIImage (ERCategory)
  10. + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
  11. {
  12. CGRect rect=CGRectMake(0, 0, size.width, size.height);
  13. UIGraphicsBeginImageContext(rect.size);
  14. CGContextRef context = UIGraphicsGetCurrentContext();
  15. CGContextSetFillColorWithColor(context, [color CGColor]);
  16. CGContextFillRect(context, rect);
  17. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  18. UIGraphicsEndImageContext();
  19. return image;
  20. }
  21. + (UIImage *)setCornerWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius{
  22. UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, image.size.width, image.size.height) cornerRadius:cornerRadius];
  23. UIGraphicsBeginImageContext(image.size);
  24. CGContextRef ctx = UIGraphicsGetCurrentContext();
  25. CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
  26. CGContextAddPath(ctx, path.CGPath);
  27. CGContextClip(ctx);
  28. [image drawInRect:rect];
  29. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  30. UIGraphicsEndImageContext();
  31. return newImage;
  32. }
  33. + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)cornerRadius{
  34. UIImage *image = [self imageWithColor:color size:size];
  35. UIImage *newImage = [self setCornerWithImage:image cornerRadius:cornerRadius];
  36. return newImage;
  37. }
  38. @end