事例代碼:
- (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);