问题描述
我到处搜索但没有找到解决方案。我有图像1.我怎样才能以渐变的方式用渐变色调来获得图像2和3?以下是这些图片:
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:
我通过Photoshop应用于它们的色调是简单的双色线性渐变。
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?
解决方案: jrtc27给了我几乎工作示例。我修复它(对于ARC)并使其可重用(使用UIImage的类别)。这是:
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;
}
推荐答案
我相信以下应该有效 - 如果没有则做评论!
I believe that the following should work - do comment if it doesn't!
// Load image
UIImage *image = [UIImage imageNamed:@"MyCoolImage.png"];
CGFloat scale = image.scale;
UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scale, image.size.height * scale));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, image.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeMultiply);
CGRect rect = CGRectMake(0, 0, image.size.width * scale, image.size.height * scale);
CGContextDrawImage(context, rect, image.CGImage);
// Create gradient
UIColor *colorOne = ....;
UIColor *colorTwo = ....;
NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, (id)colorTwo.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);
// Apply gradient
CGContextClipToMask(context, rect, image.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0,image.size.height * scale), 0);
CGGradientRelease(gradient);
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
信用获取基本想法。
这篇关于我怎样才能用渐变色调UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!