Iphone 更改UIImage按钮的颜色
我想更改已作为按钮放置在导航栏中的图像的颜色。我用来创建图像/按钮的代码是:Iphone 更改UIImage按钮的颜色,iphone,ios,objective-c,uibutton,uiappearance,Iphone,Ios,Objective C,Uibutton,Uiappearance,我想更改已作为按钮放置在导航栏中的图像的颜色。我用来创建图像/按钮的代码是: UIImage *btnGoImage = [UIImage imageNamed:@"settings_cog.png"]; UIButton *btnGoPre = [UIButton buttonWithType:UIButtonTypeCustom]; btnGoPre.bounds = CGRectMake( 0, 0, 30, 30 ); [btnGoPre setImage:btnGoImage forS
UIImage *btnGoImage = [UIImage imageNamed:@"settings_cog.png"];
UIButton *btnGoPre = [UIButton buttonWithType:UIButtonTypeCustom];
btnGoPre.bounds = CGRectMake( 0, 0, 30, 30 );
[btnGoPre setImage:btnGoImage forState:UIControlStateNormal];
[btnGoPre addTarget:self action:@selector(loginAction)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithCustomView:btnGoPre];
如果我想将图像更改为红色(当前为灰色)。我该怎么做?谢谢 这是一种有助于更改图像颜色的方法,您需要将图像和颜色作为输入变量传递,然后返回输出图像
-(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
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;
}
下面是如何调用此方法的示例
yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
这是一种有助于更改图像颜色的方法,您需要将图像和颜色作为输入变量传递,然后返回输出图像
-(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
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;
}
下面是如何调用此方法的示例
yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
这是一种有助于更改图像颜色的方法,您需要将图像和颜色作为输入变量传递,然后返回输出图像
-(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
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;
}
下面是如何调用此方法的示例
yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
这是一种有助于更改图像颜色的方法,您需要将图像和颜色作为输入变量传递,然后返回输出图像
-(UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
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;
}
下面是如何调用此方法的示例
yourImageView.image = [self imageNamed:@"yourImageName" withColor:[UIColor orangeColor]];
尝试UIBarButtonItem类tintColor属性。尝试UIBarButtonItem类tintColor属性。尝试UIBarButtonItem类tintColor属性。尝试UIBarButtonItem类tintColor属性