【转】代码让UIImage具有渐变效果

本文介绍了一个UIImage的category,用于在iOS应用中为图片添加背景颜色和阴影效果。通过自定义参数,如背景颜色、阴影透明度和模糊度,可以轻松地创建带有阴影的图片,增强UI视觉效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://blog.sina.com.cn/s/blog_7ccde1bf0100v9ni.html

 

做一个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];

 

转载于:https://www.cnblogs.com/yencain/articles/2514554.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值