如何使用渐变为 UIImage 着色?
我到处搜索但没有找到解决方案。我有图像 1。如何以编程方式使用渐变对它们进行着色以获得图像 2 和 3?以下是这些图像:
我通过 Photoshop 应用到它们的色调是简单的 2 色线性渐变。
我的问题是:我怎样才能以编程方式实现这种效果?
解决方案: jrtc27 给了我几乎工作示例。我修复了它(对于 ARC)并使其可重用(使用 UIImage 的类别)。这是它:
- (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArr {
CGFloat scale = self.scale;
UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width * scale, self.size.height * scale);
CGContextDrawImage(context, rect, self.CGImage);
// Create gradient
UIColor *colorOne = [colorsArr objectAtIndex:1]; // top color
UIColor *colorTwo = [colorsArr objectAtIndex:0]; // bottom color
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
// Apply gradient
CGContextClipToMask(context, rect, self.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,self.size.height * scale), 0);
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return gradientImage;
}
I searched everywhere but didn't find the solution. I have image 1. How can I programatically tint them with gradient to get images 2 and 3? Here are those images:
Tints that I applied to them via Photoshop are simple 2-color linear gradients.
And my question is: how can I achieve this effect programatically?
Solution: jrtc27 gave me almost working example. I fixed it (for ARC) and made it reusable (using UIImage's category). Here is it:
- (UIImage *)tintedWithLinearGradientColors:(NSArray *)colorsArr {
CGFloat scale = self.scale;
UIGraphicsBeginImageContext(CGSizeMake(self.size.width * scale, self.size.height * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width * scale, self.size.height * scale);
CGContextDrawImage(context, rect, self.CGImage);
// Create gradient
UIColor *colorOne = [colorsArr objectAtIndex:1]; // top color
UIColor *colorTwo = [colorsArr objectAtIndex:0]; // bottom color
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
// Apply gradient
CGContextClipToMask(context, rect, self.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,self.size.height * scale), 0);
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return gradientImage;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:这是一个支持非视网膜和视网膜显示的版本
该方法可以用作
UIImage
的类别编辑:由 sobri 添加更改
EDIT: Here is a version which supports non-retina and retina displays
The method can be used as a category for
UIImage
Edit: added change by sobri
斯威夫特 4 版本。
Swift 4 version.
我相信以下内容应该有效 - 如果无效请发表评论!
基本思想归功于 CoffeeShopped。
I believe that the following should work - do comment if it doesn't!
Credit to CoffeeShopped for the basic idea.
Swift 版本(作为 UIImage 扩展,使用雷米的答案):
Swift version (as UIImage extension, using remy's answer ):
类 ViewController: UIViewController {
}
扩展 UIImage {
func maskWithGradientColor(颜色: UIColor) ->用户界面图像? {
}
class ViewController: UIViewController {
}
extension UIImage {
func maskWithGradientColor(color: UIColor) -> UIImage? {
}