66 lines
2.2 KiB
Objective-C
Executable File
66 lines
2.2 KiB
Objective-C
Executable File
//
|
||
// UIImage+Gradient.m
|
||
// testLayer
|
||
//
|
||
// Created by tb on 17/3/17.
|
||
// Copyright © 2017年 com.tb. All rights reserved.
|
||
//
|
||
|
||
#import "UIImage+Gradient.h"
|
||
|
||
@implementation UIImage (Gradient)
|
||
|
||
- (UIImage *)createImageWithSize:(CGSize)imageSize gradientColors:(NSArray *)colors percentage:(NSArray *)percents gradientType:(GradientType)gradientType {
|
||
|
||
NSAssert(percents.count <= 5, @"输入颜色数量过多,如果需求数量过大,请修改locations[]数组的个数");
|
||
|
||
NSMutableArray *ar = [NSMutableArray array];
|
||
for(UIColor *c in colors) {
|
||
[ar addObject:(id)c.CGColor];
|
||
}
|
||
|
||
CGFloat locations[5];
|
||
for (int i = 0; i < percents.count; i++) {
|
||
locations[i] = [percents[i] floatValue];
|
||
}
|
||
|
||
|
||
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 1);
|
||
CGContextRef context = UIGraphicsGetCurrentContext();
|
||
CGContextSaveGState(context);
|
||
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
|
||
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, locations);
|
||
CGPoint start;
|
||
CGPoint end;
|
||
switch (gradientType) {
|
||
case GradientFromTopToBottom:
|
||
start = CGPointMake(imageSize.width/2, 0.0);
|
||
end = CGPointMake(imageSize.width/2, imageSize.height);
|
||
break;
|
||
case GradientFromLeftToRight:
|
||
start = CGPointMake(0.0, imageSize.height/2);
|
||
end = CGPointMake(imageSize.width, imageSize.height/2);
|
||
break;
|
||
case GradientFromLeftTopToRightBottom:
|
||
start = CGPointMake(0.0, 0.0);
|
||
end = CGPointMake(imageSize.width, imageSize.height);
|
||
break;
|
||
case GradientFromLeftBottomToRightTop:
|
||
start = CGPointMake(0.0, imageSize.height);
|
||
end = CGPointMake(imageSize.width, 0.0);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
|
||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
||
|
||
CGGradientRelease(gradient);
|
||
CGContextRestoreGState(context);
|
||
|
||
UIGraphicsEndImageContext();
|
||
return image;
|
||
}
|
||
|
||
@end
|