事例代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
img.image = [self selectedImage:CGSizeMake(200, 200)];
[self.view addSubview:img];
}
- (UIImage *) selectedImage: (CGSize) size
{
const CGFloat locations[] = {0,0.3,0.6,1};
const CGFloat components[] = {
0.0, 0.0, 0.0, 1, //黑色
0.0, 0.0, 1.0, 1, //蓝色
0.5, 0.0, 0.5, 1, //紫色
1.0, 1.0 ,1.0 ,1, //白色
};
return [self gradientImageWithSize:size locations:locations components:components count:3];
}
- (UIImage *) gradientImageWithSize:(CGSize) size
locations:(const CGFloat []) locations
components:(const CGFloat []) components
count:(NSUInteger)count
{
UIGraphicsBeginImageContextWithOptions(size, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGGradientRef colorGradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 4);
CGColorSpaceRelease(colorSpace);
CGContextDrawLinearGradient(context, colorGradient, (CGPoint){0, 0}, (CGPoint){size.width, 0}, 0);
CGGradientRelease(colorGradient);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
结果展示:
核心方法解释:
/**
从颜色空间和提供的颜色组件和位置创建CGGradient对象。
@param colorSpace The color space to use for the gradient. You cannot use a pattern or indexed color space.(颜色空间)
@param components The color components for each color that defines the gradient. The components should be in the color space specified by space. If you are unsure of the number of components, you can call the function CGColorSpaceGetNumberOfComponents.
The number of items in this array should be the product of count and the number of components in the color space. For example, if the color space is an RGBA color space and you want to use two colors in the gradient (one for a starting location and another for an ending location), then you need to provide 8 values in components—red, green, blue, and alpha values for the first color, followed by red, green, blue, and alpha values for the second color.(对应的颜色分量值数组,颜色分量需要和颜色空间对应)
@param locations The location for each color provided in components. Each location must be a CGFloat value in the range of 0 to 1, inclusive. If 0 and 1 are not in the locations array, Quartz uses the colors provided that are closest to 0 and 1 for those locations.
If locations is NULL, the first color in colors is assigned to location 0, the last color incolors is assigned to location 1, and intervening colors are assigned locations that are at equal intervals in between.(颜色分量的位置数组)
@param 4 The number of locations provided in the locations parameters.
@return A CGGradient object.
渲染方式是从components数组的第一颜色变量开始,渐变到第二个颜色分量,范围是从0开始到locations数组中第一个值,然后从此位置到第二个值,颜色渐变到对应的第二个值,依次类推,直到最后。如果locations数组的起始位置不是0,则locations数组第一个值之前的位置由components数组的第一颜色变量填充。如果locations数组的最后位置不是1,则locations数组最后值之后的位置由components数组的最后颜色变量填充。
*/
CG_EXTERN CGGradientRef __nullable CGGradientCreateWithColorComponents(
CGColorSpaceRef cg_nullable space, const CGFloat * cg_nullable components,
const CGFloat * __nullable locations, size_t count)
CG_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);