I have an image that is an almost flat color. It's attached but, plain white so you can't see it easily.
我有一个几乎是平面颜色的图像。它附着但是纯白色,所以你不能轻易看到它。
I would like to be able to dynamically color this image at runtime, but I need to do it in iOS 6 without using UIImageRenderingModeAlwaysTemplate.
我希望能够在运行时动态地为该图像着色,但我需要在iOS 6中不使用UIImageRenderingModeAlwaysTemplate。
The images will all start plain white with minor gradients for rounded corners.
所有图像都将以纯白色开始,并带有圆角的小渐变。
So far my best attempt has been using GPUImage and a category on UIImage
到目前为止,我最好的尝试是在UIImage上使用GPUImage和一个类别
@implementation UIImage (BPAdditions)
- (UIImage *)imageWithColor:(UIColor *)color
{
GPUImageRGBFilter *stillImageFilter = [[GPUImageRGBFilter alloc] init];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha =0.0;
[color getRed:&red green:&green blue:&blue alpha:&alpha];
stillImageFilter.red = red;
stillImageFilter.green = green;
stillImageFilter.blue = blue;
GPUImageChromaKeyFilter *stillImageFilter2 = [[GPUImageChromaKeyFilter alloc] init];
[stillImageFilter2 setColorToReplaceRed:0 green:0 blue:0];
stillImageFilter2.thresholdSensitivity = 0.2;
stillImageFilter2.smoothing = 0;
UIImage *img = [stillImageFilter imageByFilteringImage:self];
return [stillImageFilter2 imageByFilteringImage: img];
}
This would be ideal, except when I used the RGB filter it turns the clear background black. Then removing this with the chroma filter has varying quality depending on the color used.
这将是理想的,除非我使用RGB滤镜时将透明背景变为黑色。然后使用色度滤光片将其去除根据使用的颜色具有不同的质量。
There is a possibility that the target color will be black, in which case this solution will fail entirely.
目标颜色可能是黑色,在这种情况下,此解决方案将完全失败。
1 个解决方案
#1
18
Not sure if I understand what you're trying to achieve, but do you want to add a simple color tint to the base image? If so, you can achieve this with the following category on UIImage:
不确定我是否理解您要实现的目标,但是您想为基本图像添加简单的颜色色调吗?如果是这样,您可以在UIImage上使用以下类别实现此目的:
- (UIImage *)tintWithColor:(UIColor *)tintColor {
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height);
[self drawInRect:drawRect];
[tintColor set];
UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
-- Update: Swift 2 version --
- 更新:Swift 2版本 -
extension UIImage {
func tintWithColor(color:UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.mainScreen().scale)
let rect = CGRectMake(0, 0, self.size.width, self.size.height)
self.drawInRect(rect)
color.setFill()
UIRectFillUsingBlendMode(rect, CGBlendMode.SourceAtop)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}
#1
18
Not sure if I understand what you're trying to achieve, but do you want to add a simple color tint to the base image? If so, you can achieve this with the following category on UIImage:
不确定我是否理解您要实现的目标,但是您想为基本图像添加简单的颜色色调吗?如果是这样,您可以在UIImage上使用以下类别实现此目的:
- (UIImage *)tintWithColor:(UIColor *)tintColor {
UIGraphicsBeginImageContextWithOptions(self.size, NO, [[UIScreen mainScreen] scale]);
CGRect drawRect = CGRectMake(0, 0, self.size.width, self.size.height);
[self drawInRect:drawRect];
[tintColor set];
UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return tintedImage;
}
-- Update: Swift 2 version --
- 更新:Swift 2版本 -
extension UIImage {
func tintWithColor(color:UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, UIScreen.mainScreen().scale)
let rect = CGRectMake(0, 0, self.size.width, self.size.height)
self.drawInRect(rect)
color.setFill()
UIRectFillUsingBlendMode(rect, CGBlendMode.SourceAtop)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
}