UIImage+Tint.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // UIImage+Tint.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/3/6.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "UIImage+Tint.h"
  9. @implementation UIImage (Tint)
  10. -(UIImage *)imageWithTintColor:(UIColor *)tintColor{
  11. return [self imageWithTintColor:tintColor blendMode:kCGBlendModeDestinationIn];
  12. }
  13. -(UIImage *)imageWithGradientTintColor:(UIColor *)tintColor{
  14. return [self imageWithTintColor:tintColor blendMode:kCGBlendModeOverlay];
  15. }
  16. -(UIImage *)imageWithTintColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode{
  17. //We want to keep alpha, set opaque to NO; Use 0.0f for scale to use the scale factor of the device’s main screen.
  18. UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0f);
  19. [tintColor setFill];
  20. CGRect bounds = CGRectMake(0, 0, self.size.width, self.size.height);
  21. UIRectFill(bounds);
  22. //Draw the tinted image in context
  23. [self drawInRect:bounds blendMode:blendMode alpha:1.0f];
  24. if (blendMode != kCGBlendModeDestinationIn) {
  25. [self drawInRect:bounds blendMode:kCGBlendModeDestinationIn alpha:1.0f];
  26. }
  27. UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
  28. UIGraphicsEndImageContext();
  29. return tintedImage;
  30. }
  31. // 控件绘制成图片
  32. + (UIImage *)imageWithView:(UIView *)view {
  33. //转化成image
  34. // UIGraphicsBeginImageContext(view.bounds.size);
  35. UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES,[UIScreen mainScreen].scale);
  36. CGContextRef ctx = UIGraphicsGetCurrentContext();
  37. [view.layer renderInContext:ctx];
  38. UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
  39. UIGraphicsEndImageContext();
  40. return tImage;
  41. }
  42. @end