您可以通过应用单一颜色的渐变来实现这一点。下面的代码执行了我们需要的相同操作
- (UIImage *)applyColor:(UIColor *)color toImage:(UIImage*)toImage{
UIGraphicsBeginImageContextWithOptions(toImage.size, NO, toImage.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, toImage.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, toImage.size.width, toImage.size.height);
NSArray *colors = [NSArray arrayWithObjects:(id)color.CGColor, (id)color.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
CGContextClipToMask(context, rect, toImage.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, toImage.size.height), 0);
UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGGradientRelease(gradient);
CGColorSpaceRelease(space);
return coloredImage;
}
const float myMaskingColors[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};
- Buntylm