[英]Applying gradient to UIImage - how to remove color invertion?
I'm applying gradient to UIImage
, all i want it to be dark on the bottom and slowly turn to clear or light gray in the middle. 我正在对
UIImage
应用渐变,我想让它在底部变暗,然后在中间慢慢变成透明或浅灰色。 It is mostly ok, but i have a problem that my image colors invert in some places under this gradient. 它基本上没问题,但我有一个问题,我的图像颜色在这个渐变下的某些地方反转。 This looks very annoying.
这看起来很烦人。 How do i resolve that?
我该如何解决?
Here is my method for your convenience. 这是我方便的方法。
I have tried picking different blending modes, but it doesn't help. 我尝试过采用不同的混合模式,但它没有帮助。 Another question, is how do I make black more intensive in the bottom?
另一个问题是,如何在底部使黑色更加密集?
This method will work without any extra code. 此方法无需任何额外代码即可运行。 Start color is
[UIColor blackColor]
and end color is [UIColor clearColor]
: 开始颜色为
[UIColor blackColor]
,结束颜色为[UIColor clearColor]
:
- (UIImage *)imageWithGradient:(UIImage *)img startColor:(UIColor *)color1 endColor:(UIColor *)color2 {
UIGraphicsBeginImageContextWithOptions(img.size, NO, img.scale);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeDarken); // tried normal here, same results
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// Create gradient
NSArray *colors = [NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);
// Apply gradient
CGContextClipToMask(context, rect, img.CGImage);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, img.size.height / 2.0), 0);
UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGGradientRelease(gradient);
CGColorSpaceRelease(space);
return gradientImage;
}
Try this 尝试这个
// Set image over layer
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = imageView.frame;
// Add colors to layer
UIColor *centerColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.25];
UIColor *endColor = [UIColor grayColor];
gradient.colors = [NSArray arrayWithObjects:
(id)[endColor CGColor],
(id)[centerColor CGColor],
(id)[endColor CGColor],
nil];
[imageView.layer insertSublayer:gradient atIndex:0];
Just change array of gradient.colors depend on your requirements. 只需更改gradient.colors数组取决于您的要求。
Navnath answer in Swift 纳瓦特在斯威夫特回答
var gradient = CAGradientLayer()
gradient.frame = imageView.frame
gradient.colors = [
UIColor(white: 0, alpha: 0.25).CGColor,
UIColor.grayColor().CGColor,
UIColor(white: 0, alpha: 0.25).CGColor
]
imageView.layer.insertSublayer(gradient, atIndex: 0)
Hope you find it useful 希望你觉得它有用
对Navnath和Francis的答案稍作修正...... gradient.frame需要设置为imageView.bounds:
gradient.frame = imageView.bounds
Try just changing the alpha to get the desired result. 尝试更改alpha以获得所需的结果。 Place the layer over top of your existing image.
将图层放在现有图像的顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.