Ios 彩色按钮图像

Ios 彩色按钮图像,ios,iphone,uibutton,ios7,uisegmentedcontrol,Ios,Iphone,Uibutton,Ios7,Uisegmentedcontrol,我注意到,当我将白色或黑色的UIImage放入UISegmentedControl时,它会自动对其进行颜色遮罩,以匹配分段控件的色调。我觉得这真的很酷,想知道我是否可以在其他地方也这样做。例如,我有一堆形状统一但颜色各异的按钮。除了为每个按钮制作PNG外,我是否可以使用此颜色掩蔽为所有按钮使用相同的图像,然后设置着色颜色或其他更改按钮实际颜色的内容?您应该尝试一下 定格后 NSArray *arr10 =[NSArray arrayWithObjects:btn1,btn2,nil]; for(

我注意到,当我将白色或黑色的
UIImage
放入
UISegmentedControl
时,它会自动对其进行颜色遮罩,以匹配分段控件的色调。我觉得这真的很酷,想知道我是否可以在其他地方也这样做。例如,我有一堆形状统一但颜色各异的按钮。除了为每个按钮制作PNG外,我是否可以使用此颜色掩蔽为所有按钮使用相同的图像,然后设置着色颜色或其他更改按钮实际颜色的内容?

您应该尝试一下

定格后

NSArray *arr10 =[NSArray arrayWithObjects:btn1,btn2,nil];
for(UIButton *btn10 in arr10)
{
CAGradientLayer *btnGradient2 = [CAGradientLayer layer];
btnGradient2.frame = btn10.bounds;

btnGradient2.colors = [NSArray arrayWithObjects:
                       (id)[[UIColor colorWithRed:151.0/255.0f green:206.0/255.5 blue:99.0/255.0 alpha:1] CGColor],
                       (id)[[UIColor colorWithRed:126.0/255.0f green:192.0/255.5 blue:65.0/255.0 alpha:1]CGColor],
                       nil];
[btn10.layer insertSublayer:btnGradient2 atIndex:0];

}

不确定您到底想要什么,但此分类方法将使用指定的颜色屏蔽UIImage,以便您可以拥有单个图像并将其颜色更改为您想要的任何颜色

ImageUtils.h

- (UIImage *) maskWithColor:(UIColor *)color;
ImageUtils.m

-(UIImage *) maskWithColor:(UIColor *)color 
{
    CGImageRef maskImage = self.CGImage;
    CGFloat width = self.size.width;
    CGFloat height = self.size.height;
    CGRect bounds = CGRectMake(0,0,width,height);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
    CGContextClipToMask(bitmapContext, bounds, maskImage);
    CGContextSetFillColorWithColor(bitmapContext, color.CGColor);    
    CGContextFillRect(bitmapContext, bounds);

    CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
    UIImage *coloredImage = [UIImage imageWithCGImage:cImage];

    CGContextRelease(bitmapContext);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cImage);

    return coloredImage;    
}
导入ImageUtils类别并执行以下操作

#import "ImageUtils.h"

...

UIImage *icon = [UIImage imageNamed:ICON_IMAGE];

UIImage *redIcon = [icon maskWithColor:UIColor.redColor];
UIImage *blueIcon = [icon maskWithColor:UIColor.blueColor];

从iOS 7开始,在
UIImage
上有一种新方法来指定渲染模式。使用渲染模式
UIImageRenderingModeAlwaysTemplate
将允许通过按钮的着色颜色控制图像颜色

目标-C

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image = [[UIImage imageNamed:@"image_name"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[button setImage:image forState:UIControlStateNormal]; 
button.tintColor = [UIColor redColor];
迅捷的


正如Ric在他的文章中已经提到的,您可以在代码中设置渲染模式,也可以直接在图像目录中进行设置,请参见下面的附加图像。只需将
渲染为
设置为
模板图像


警告我对iOS 7和这种方法有问题。因此,如果您也使用iOS 7,您可能也希望在代码中这样做,以确保如前所述。

您必须将图像渲染模式设置为
UIImageRenderingModeAlwaysTemplate
,以便
tintColor
影响UIImage。以下是Swift中的解决方案:

let image = UIImage(named: "image-name")
let button = UIButton()
button.setImage(image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate), forState: .Normal)
button.tintColor = UIColor.whiteColor()
SWIFT 4x

button.setImage(image.withRenderingMode(UIImage.RenderingMode.alwaysTemplate), for: .normal)
button.tintColor = UIColor.blue

在Swift中,您可以这样做:

var exampleImage = UIImage(named: "ExampleImage.png")?.imageWithRenderingMode(.AlwaysTemplate)
然后在你的视图中加载

exampleButtonOutlet.setImage(exampleImage, forState: UIControlState.Normal)
和修改颜色

exampleButtonOutlet.tintColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1) //your color
编辑Xcode 8
现在,您还可以将.xcsets中图像的渲染模式转换为模板图像,然后无需再在
var exampleImage
中明确声明它

如果您想手动屏蔽图像,下面是适用于视网膜屏幕的更新代码

- (UIImage *)maskWithColor:(UIColor *)color
{
    CGImageRef maskImage = self.CGImage;
    CGFloat width = self.size.width * self.scale;
    CGFloat height = self.size.height * self.scale;
    CGRect bounds = CGRectMake(0,0,width,height);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);
    CGContextClipToMask(bitmapContext, bounds, maskImage);
    CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
    CGContextFillRect(bitmapContext, bounds);

    CGImageRef cImage = CGBitmapContextCreateImage(bitmapContext);
    UIImage *coloredImage = [UIImage imageWithCGImage:cImage scale:self.scale orientation:self.imageOrientation];

    CGContextRelease(bitmapContext);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(cImage);

    return coloredImage;
}

自定义按钮以各自的图像颜色显示。在情节提要中将按钮类型设置为“系统”(或在代码中将按钮类型设置为“UIButtonTypeSystem”)将使用默认的着色颜色渲染按钮的图像


(在iOS9、Xcode 7.3上测试)

Swift 3.0

    let image = UIImage(named:"NoConnection")!

 warningButton = UIButton(type: .system)        
    warningButton.setImage(image, for: .normal)
    warningButton.tintColor = UIColor.lightText
    warningButton.frame = CGRect(origin: CGPoint(x:-100,y:0), size: CGSize(width: 59, height: 56))

    self.addSubview(warningButton)

带有自定义类型的Swift 4:

let button = UIButton(frame: aRectHere)
    let buttonImage = UIImage(named: "imageName")
    button.setImage(buttonImage?.withRenderingMode(.alwaysTemplate), for: .normal)
    button.tintColor = .white
对于Xamarin.iOS(C#):


如果您有一个带有背景图像的自定义按钮。您可以设置按钮的着色颜色,并用以下内容覆盖该图像

在“资源”中,选择要设置着色颜色的按钮背景

在图像的属性检查器中,将“渲染”值设置为“模板图像”


现在,无论何时设置
button.tintColor=UIColor.red
按钮,按钮都将显示为红色

更改按钮图像或图像视图着色颜色:

btn.imageView?.image = btn.imageView?.image?.withRenderingMode(.alwaysTemplate)

btn.imageView?.tintColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)

Swift 3

如果您已经通过xCode interface builder设置了映像,那么这个解决方案可能会比较舒服。基本上,您有一个扩展来为图像着色:

extension UIImage {
    public func image(withTintColor color: UIColor) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
        let context: CGContext = UIGraphicsGetCurrentContext()!
        context.translateBy(x: 0, y: self.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(CGBlendMode.normal)
        let rect: CGRect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height)
        context.clip(to: rect, mask: self.cgImage!)
        color.setFill()
        context.fill(rect)
        let newImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
}
然后,您可以准备此ui按钮扩展,为特定状态的图像着色:

extension UIButton {
    func imageWith(color:UIColor, for: UIControlState) {
        if let imageForState = self.image(for: state) {
            self.image(for: .normal)?.withRenderingMode(.alwaysTemplate)
            let colorizedImage = imageForState.image(withTintColor: color)
            self.setImage(colorizedImage, for: state)
        }
    }
}
用法:

myButton.imageWith(.red, for: .normal)

另外,(在表格单元格中也可以正常工作,您不需要调用
setNeedDisplay()
方法,颜色的更改是由于UIImage扩展名而立即发生的。

以上所有操作对我都不起作用,因为单击后色调被清除。我必须使用

button.setImageTintColor(Palette.darkGray(), for: UIControlState())

要在按钮上设置图像的白色(箭头图标),我们使用:

let imageOnButton = UIImage(named: "navForwardArrow")?.imageWithColor(color: UIColor.white)
button.setImage(imageOnButton, for: .normal)
已知问题:按下按钮时图标会失去白色

截图:


如果您正在通过
UIColor(r:g:b:alpha:)设置
UIButton.tintColor
,记住将值除以
255
。这些RGB值应该在0和1之间。

您可以发布您想要使用的图像,并发布所需结果的图像吗?这与
cgbitmappalphainfomask和kcgimagealphapremultipledast上的interface builder使用
cgbitmappalphainfomask和kgimagealphapremultipleedlast
相同,但需要要调整视网膜图形…当我在视网膜图像上运行此操作时,它会返回一个非视网膜图像。要支持视网膜设备,可以使用
UIDevice
class:
CGFloat scale=[UIScreen mainScreen]的scale属性.scale;CGFloat width=self.size.width*scale;CGFloat height=self.size.height*scale;
scale
属性从iOS 4.0开始就存在。创建
UIImage
时,还需要使用
scale
值:
UIImage*coloredImage=[UIImage imageWithCGImage:cImage比例:比例方向:self.imageOrientation];
我认为最好使用button.imageView。tintColor@M.Othman仅当我使用button.tintColor而不是button.imageview.tintColor时适用于我只要记住在xcassets per@hashier中设置图像渲染模式创建UIButtonTypeSystem类型的UIButton系统将自动使用您设置的tintColor。如果
tintColor
太暗,请确保
adjustsImageWhenHighlighted
为否(或在IB中:“突出显示的调整图像”未选中)。作为旁注,图像文件应为黑色透明(非黑白)@AxelGuilmin并非如此。您的图像可以是您想要的任何颜色和透明的。任何不同于透明的颜色都将转换为通用颜色,然后默认情况下,它将以黑色着色。如果您在这里,您可以转到此链接:并转到模板图像(作为说明)这是一个更好的解决方案,因为它减少了代码,并且与公认的答案做了几乎相同的事情。我认为这应该是正确的解决方案
button.setImageTintColor(Palette.darkGray(), for: UIControlState())
let imageOnButton = UIImage(named: "navForwardArrow")?.imageWithColor(color: UIColor.white)
button.setImage(imageOnButton, for: .normal)
let button = UIButton(type: .custom)
let image = UIImage(named: "image_name")?.withRenderingMode(.alwaysTemplate)
button.setImage(image, for: .normal)
button.tintColor = UIColor.red