UIImage具有渐变效果

做一个UIImage的category,直接上代码:
- (UIImage *) imageWithBackgroundColor
:(UIColor *)bgColor
                                                    shadeAlpha1:(CGFloat)alpha1
                                                    shadeAlpha2:(CGFloat)alpha2
                                                    shadeAlpha3:(CGFloat)alpha3
                                                    shadowColor:(UIColor *)shadowColor
                                                  shadowOffset:(CGSize)shadowOffset
                                                      shadowBlur:(CGFloat)shadowBlur {
      UIImage*image = self;
      CGColorRefcgColor = [bgColor CGColor];
      CGColorRefcgShadowColor = [shadowColor CGColor];
      CGFloatcomponents[16] ={1,1,1,alpha1,1,1,1,alpha1,1,1,1,alpha2,1,1,1,alpha3};
      CGFloatlocations[4] = {0,0.5,0.6,1};
      CGColorSpaceRef colorSpace =CGColorSpaceCreateDevice RGB(); 
      CGGradientRef colorGradient =CGGradientCreateWithColo rComponents(colorSpace, components,locations, (size_t)4);
      CGRectcontextRect;
      contextRect.origin.x = 0.0f;
      contextRect.origin.y = 0.0f;
      contextRect.size = [image size];
      //contextRect.size = CGSizeMake([image size].width+5,[imagesize].height+5); 
      // Retrievesource image and begin image context
      UIImage*itemImage = image;
      CGSizeitemImageSize = [itemImage size];
      CGPointitemImagePosition;
      itemImagePosition.x = ceilf((contextRect.size.width -itemImageSize.width) / 2);
      itemImagePosition.y = ceilf((contextRect.size.height -itemImageSize.height) / 2);
      UIGraphicsBeginImageCont ext(contextRect.size);
      CGContextRefc = UIGraphicsGetCurrentCont ext();
      // Setupshadow
      CGContextSetShadowWithCo lor(c, shadowOffset, shadowBlur,cgShadowColor);
      // Setuptransparency layer and clip to mask
      CGContextBeginTransparen cyLayer(c, NULL);
      CGContextScaleCTM(c, 1.0, -1.0);
      CGContextClipToMask(c, CGRectMake(itemImagePosition.x,-itemImagePosition.y, itemImageSize.width, -itemImageSize.height),[itemImage CGImage]);
      // Fill andend the transparency layer
      CGContextSetFillColorWit hColor(c,cgColor);       
      contextRect.size.height = -contextRect.size.height;
      CGContextFillRect(c, contextRect);
      CGContextDrawLinearGradi ent(c,colorGradient,CGPointZero,CGPointMake(contextRect.size.width*1.0/4.0,contextRect.size.height),0);
      CGContextEndTransparency Layer(c);
      //CGPointMake(contextRect.size.width*3.0/4.0, 0)
      // Setselected image and end context
      UIImage*resultImage = UIGraphicsGetImageFromCu rrentImageContext();
      UIGraphicsEndImageContex t();
      CGColorSpaceRelease(colorSpace);
      CGGradientRelease(colorGradient);
      returnresultImage;
}

用法如下


UIImage *niceImage = [[UIImage imageNamed:@"image_name"]imageWithBackgroundColor :[UIColor colorWithRed:41.0/255.0green:147.0/255.0 blue:239.0/255.0 alpha:1.0]
                                                                                                                                          shadeAlpha1:0.6
                                                                                                                                          shadeAlpha2:0.0
                                                                                                                                          shadeAlpha3:0.4
                                                                                                                                          shadowColor:[UIColor blackColor]
                                                                                                                                        shadowOffset:CGSizeMake(0.0f, -1.0f) 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值