利用image属性renderingMode
clearButton.tintColor =[UIColorblackColor];
UIImage *image =[UIImageimageNamed:@"delete"];
image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[clearButton setImage:imageforState:UIControlStateNormal];
iOS7以上可用。
也可以重写UIImage
//改变图片颜色
- (UIImage *)imageWithColor:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
CGContextRefcontext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
[color setFill];
CGContextFillRect(context, rect);
UIImage*newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
UIColor 转UIImage 根据上下文
- (UIImage*) createImageWithColor: (UIColor*) color
{
CGRect rect=CGRectMake(0,0, 1, 1);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}
UIImage转UIColor
[UIColor colorWithPatternImage:[UIImageimageNamed:@“bg.png"]]