Chinaunix首页 | 论坛 | 博客
  • 博客访问: 894638
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: LINUX

2012-06-04 15:19:55

做一个UIImage的category,直接上代码:

  1. - (UIImage *) imageWithBackgroundColor:(UIColor *)bgColor   
  2.                            shadeAlpha1:(CGFloat)alpha1   
  3.                            shadeAlpha2:(CGFloat)alpha2  
  4.                            shadeAlpha3:(CGFloat)alpha3   
  5.                            shadowColor:(UIColor *)shadowColor   
  6.                           shadowOffset:(CGSize)shadowOffset   
  7.                             shadowBlur:(CGFloat)shadowBlur {   
  8.     UIImage *image = self;  
  9.     CGColorRef cgColor = [bgColor CGColor];  
  10.     CGColorRef cgShadowColor = [shadowColor CGColor];  
  11.     CGFloat components[16] = {1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};  
  12.     CGFloat locations[4] = {0,0.5,0.6,1};  
  13.     CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();    
  14.     CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, (size_t)4);  
  15.     CGRect contextRect;  
  16.     contextRect.origin.x = 0.0f;  
  17.     contextRect.origin.y = 0.0f;  
  18.     contextRect.size = [image size];  
  19.     //contextRect.size = CGSizeMake([image size].width+5,[image size].height+5);    
  20.     // Retrieve source image and begin image context  
  21.     UIImage *itemImage = image;  
  22.     CGSize itemImageSize = [itemImage size];  
  23.     CGPoint itemImagePosition;   
  24.     itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);  
  25.     itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);  
  26.     UIGraphicsBeginImageContext(contextRect.size);  
  27.     CGContextRef c = UIGraphicsGetCurrentContext();  
  28.     // Setup shadow  
  29.     CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);  
  30.     // Setup transparency layer and clip to mask  
  31.     CGContextBeginTransparencyLayer(c, NULL);  
  32.     CGContextScaleCTM(c, 1.0, -1.0);  
  33.     CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);  
  34.     // Fill and end the transparency layer  
  35.     CGContextSetFillColorWithColor(c, cgColor);       
  36.     contextRect.size.height = -contextRect.size.height;  
  37.     CGContextFillRect(c, contextRect);  
  38.     CGContextDrawLinearGradient(c, colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);  
  39.     CGContextEndTransparencyLayer(c);  
  40.     //CGPointMake(contextRect.size.width*3.0/4.0, 0)  
  41.     // Set selected image and end context  
  42.     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();  
  43.     UIGraphicsEndImageContext();  
  44.     CGColorSpaceRelease(colorSpace);  
  45.     CGGradientRelease(colorGradient);  
  46.     return resultImage;  
  47. }  
 

用法如下:

  1. UIImage *niceImage = [[UIImage imageNamed:@"image_name"] imageWithBackgroundColor:[UIColor colorWithRed:41.0/255.0 green:147.0/255.0 blue:239.0/255.0 alpha:1.0]   
  2.                                                                       shadeAlpha1:0.6   
  3.                                                                       shadeAlpha2:0.0   
  4.                                                                       shadeAlpha3:0.4   
  5.                                                                       shadowColor:[UIColor blackColor]   
  6.                                                                      shadowOffset:CGSizeMake(0.0f, -1.0f)    
  7.                                                                        shadowBlur:3.0];   
 

阅读(1028) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~