UIImage+CL.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // UIImage+CL.m
  3. // CLWeeklyCalendarView
  4. //
  5. // Created by Caesar on 11/12/2014.
  6. // Copyright (c) 2014 Caesar. All rights reserved.
  7. //
  8. #import "UIImage+CL.h"
  9. #import "UIColor+CL.h"
  10. #define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
  11. @implementation UIImage (CL)
  12. /**
  13. 控件背景色--可以渐变
  14. @param height <#height description#>
  15. @return <#return value description#>
  16. */
  17. + (UIImage *)calendarBackgroundImage : (float)height {
  18. UIColor *topColor = [UIColor colorWithHex:0xffffff];
  19. UIColor *bottomColor = [UIColor colorWithHex:0xffffff];
  20. return [self gradientImageWithBounds:CGRectMake(0, 0, SCREEN_WIDTH, height) colors:@[(id)[topColor CGColor], (id)[bottomColor CGColor]]];
  21. }
  22. + (UIImage *)gradientImageWithBounds:(CGRect)bounds colors:(NSArray *)colors {
  23. CALayer * bgGradientLayer = [self gradientBGLayerForBounds:bounds colors:colors];
  24. UIGraphicsBeginImageContext(bgGradientLayer.bounds.size);
  25. [bgGradientLayer renderInContext:UIGraphicsGetCurrentContext()];
  26. UIImage * bgAsImage = UIGraphicsGetImageFromCurrentImageContext();
  27. UIGraphicsEndImageContext();
  28. return bgAsImage;
  29. }
  30. + (CALayer *)gradientBGLayerForBounds:(CGRect)bounds colors:(NSArray *)colors
  31. {
  32. CAGradientLayer * gradientBG = [CAGradientLayer layer];
  33. gradientBG.frame = bounds;
  34. gradientBG.colors = colors;
  35. return gradientBG;
  36. }
  37. @end