我正在尝试在一些当前的UIImage(白色)上添加黑色叠加层。我一直在尝试使用以下代码:
[[UIColor blackColor] set];
[image drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeOverlay alpha:1.0];
但是它不起作用,而且我非常确定set不应该在那里。
答案 0 :(得分:52)
所以,将所有答案汇总到一个这里的插入式方法可以完美地从iOS 6
一直到iOS 11
使用各种图像和图标:
+ (UIImage *)filledImageFrom:(UIImage *)source withColor:(UIColor *)color{
// begin a new image context, to draw our colored image onto with the right scale
UIGraphicsBeginImageContextWithOptions(source.size, NO, [UIScreen mainScreen].scale);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, source.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, source.size.width, source.size.height);
CGContextDrawImage(context, rect, source.CGImage);
CGContextSetBlendMode(context, kCGBlendModeSourceIn);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
}
更新: Swift 3版
func filledImage(source: UIImage, fillColor: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(source.size, false, UIScreen.main.scale)
let context = UIGraphicsGetCurrentContext()
fillColor.setFill()
context!.translateBy(x: 0, y: source.size.height)
context!.scaleBy(x: 1.0, y: -1.0)
context!.setBlendMode(CGBlendMode.colorBurn)
let rect = CGRect(x: 0, y: 0, width: source.size.width, height: source.size.height)
context!.draw(source.cgImage!, in: rect)
context!.setBlendMode(CGBlendMode.sourceIn)
context!.addRect(rect)
context!.drawPath(using: CGPathDrawingMode.fill)
let coloredImg : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return coloredImg
}
答案 1 :(得分:31)
您需要将上下文剪切为图像蒙版,然后填充纯色:
- (void)drawRect:(CGRect)rect
{
CGRect bounds = [self bounds];
[[UIColor blackColor] set];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToMask(context, bounds, [myImage CGImage]);
CGContextFillRect(context, bounds);
}
注意:myImage
应该是包含UIImage
的实例变量。我不确定它是否需要alpha通道的掩模或强度,所以请尝试两者。
答案 2 :(得分:11)
我刚刚写了一篇有助于此的教程。我的方法为您提供了UIImage的副本,其中包含您想要的颜色变化。 rpetrich的方法很棒,但要求你创建一个子类。我的方法只是几行代码,可以放在任何需要它们的地方。 http://coffeeshopped.com/2010/09/iphone-how-to-dynamically-color-a-uiimage
NSString *name = @"badge.png";
UIImage *img = [UIImage imageNamed:name];
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(img.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColorBurn);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, img.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return coloredImg;
答案 3 :(得分:8)
看看这个方法
+ (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)size;
{
UIImage *img = nil;
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,
color.CGColor);
CGContextFillRect(context, rect);
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
答案 4 :(得分:8)
从iOS 7开始,有一个更简单的解决方案:
UIImage* im = [UIImage imageNamed:@"blah"];
im = [im imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[[UIColor blackColor] setFill];
[im drawInRect:rect];
答案 5 :(得分:3)
除了rpetrich的解决方案(顺便说一句,非常棒),你还可以用以下代码替换CGContextClipToMask行:
CGContextSetBlendMode(context, kCGBlendModeSourceIn); //this is the main bit!
这是SourceIn混合模式,它通过GetCurrentContext中的任何内容来掩盖颜色。
答案 6 :(得分:0)
-set
用于设置后续绘图操作的颜色,不包括blits。我建议第一个电话,在UIView
上显示另一个(空)UIImageView
并设置其背景颜色:
myView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
显然你应该使用你想要的白色和alpha值。
答案 7 :(得分:0)
您可以通过 Swift 4.x 以编程方式通过以下方式进行操作:
imageView.image = UIImage(named: "imageName")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .purple