/**
* 获取矩形的渐变色的UIImage(此函数还不够完善)
*
* @param bounds UIImage的bounds
* @param colors 渐变色数组,可以设置两种颜色
* @param gradientType 渐变的方式:0--->从上到下 1--->从左到右
*
* @return 渐变色的UIImage
*/
- (UIImage*)gradientImageWithBounds:(CGRect)bounds andColors:(NSArray*)colors andGradientType:(int)gradientType{
NSMutableArray *ar = [NSMutableArray array];
for(UIColor *c in colors) {
[ar addObject:(id)c.CGColor];
}
UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
CGPoint start;
CGPoint end;
switch (gradientType) {
case 0:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(0.0, bounds.size.height);
break;
case 1:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(bounds.size.width, 0.0);
break;
}
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return image;
}
iOS开发:获取矩形渐变色的UIImage
最新推荐文章于 2021-11-07 20:04:06 发布