我有一个全黑的UIImage,所以有些部分是灰色的,有些部分是完全透明的。我想用这些图片作为其他颜色的遮罩(让我们说白色让它变得容易),所以最终产品现在是一个白色图像,其中部分是透明的。如何使用UIImage作为Objective-C中的颜色的遮罩Objective-C
但我完全新的石英/核芯显卡,所以我并不真的不能做的那些例子感。
有没有人知道我可以用来看一些完整的代码示例的链接?
我有一个全黑的UIImage,所以有些部分是灰色的,有些部分是完全透明的。我想用这些图片作为其他颜色的遮罩(让我们说白色让它变得容易),所以最终产品现在是一个白色图像,其中部分是透明的。如何使用UIImage作为Objective-C中的颜色的遮罩Objective-C
但我完全新的石英/核芯显卡,所以我并不真的不能做的那些例子感。
有没有人知道我可以用来看一些完整的代码示例的链接?
在iOS 7+中,您应该使用UIImageRenderingModeAlwaysTemplate
来代替。见https://stackoverflow.com/a/26965557/870313
创建从黑与-α主图像(IOS)任意颜色的图标。
// Usage: UIImage *buttonImage = [UIImage ipMaskedImageNamed:@"UIButtonBarAction.png" color:[UIColor redColor]];
+ (UIImage *)ipMaskedImageNamed:(NSString *)name color:(UIColor *)color
{
UIImage *image = [UIImage imageNamed:name];
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, image.scale);
CGContextRef c = UIGraphicsGetCurrentContext();
[image drawInRect:rect];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}
我转换此的OSX here作为类别和下面复制。请注意,这是针对非ARC项目的。对于ARC项目,autorelease可以被删除。
- (NSImage *)cdsMaskedWithColor:(NSColor *)color
{
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
NSImage *result = [[NSImage alloc] initWithSize:self.size];
[result lockFocusFlipped:self.isFlipped];
NSGraphicsContext *context = [NSGraphicsContext currentContext];
CGContextRef c = (CGContextRef)[context graphicsPort];
[self drawInRect:NSRectFromCGRect(rect)];
CGContextSetFillColorWithColor(c, [color CGColor]);
CGContextSetBlendMode(c, kCGBlendModeSourceAtop);
CGContextFillRect(c, rect);
[result unlockFocus];
return [result autorelease];
}
+ (NSImage *)cdsMaskedImageNamed:(NSString *)name color:(NSColor *)color
{
NSImage *image = [NSImage imageNamed:name];
return [image cdsMaskedWithColor:color];
}
翻译Jano的的回答到斯威夫特:
func ipMaskedImageNamed(name:String, color:UIColor) -> UIImage {
let image = UIImage(named: name)
let rect:CGRect = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: image!.size.width, height: image!.size.height))
UIGraphicsBeginImageContextWithOptions(rect.size, false, image!.scale)
let c:CGContextRef = UIGraphicsGetCurrentContext()
image?.drawInRect(rect)
CGContextSetFillColorWithColor(c, color.CGColor)
CGContextSetBlendMode(c, kCGBlendModeSourceAtop)
CGContextFillRect(c, rect)
let result:UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
用法:
myButton.setImage(ipMaskedImageNamed("grayScalePNG", color: UIColor.redColor()), forState: .Normal)
编辑:这一article,你可以把图像变成面具,其不透明的领域是以色调颜色表示。在资产目录中,在图像的Attributes Inspector下,将渲染为至模板图片。没有必要的代码。
资产目录解决方案似乎现在是真正的解决方案。它工作完美。 –
下面是斯威夫特3的变化,写为一个扩展的UIImage:
extension UIImage {
func tinted(color:UIColor) -> UIImage? {
let image = self
let rect:CGRect = CGRect(origin: CGPoint(x: 0, y: 0), size: image.size)
UIGraphicsBeginImageContextWithOptions(rect.size, false, image.scale)
let context = UIGraphicsGetCurrentContext()!
image.draw(in: rect)
context.setFillColor(color.cgColor)
context.setBlendMode(.sourceAtop)
context.fill(rect)
if let result:UIImage = UIGraphicsGetImageFromCurrentImageContext() {
UIGraphicsEndImageContext()
return result
}
else {
return nil
}
}
}
// Usage
let myImage = #imageLiteral(resourceName: "Check.png") // any image
let blueImage = myImage.tinted(color: .blue)
惊人!谢谢! –
太棒了!非常感谢! – Christophe
值得注意。如果您使用UIImageView来显示图像,这是一个简单的解决方案。 http://stackoverflow.com/a/26965557/870313 –