http://blog.csdn.net/xutianyu930818/article/details/50788268
首先,我们放个正常的图片.这个图片应该是从百度地图sdk里取得
-(void)build { self.view.backgroundColor = [UIColor blackColor]; MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(55, 55, 100, 50)]; [button setTitle:@"测试" forState:UIControlStateNormal]; UIImage *buttonImage = [UIImage imageNamed:@"dizhi"]; [button setImage:buttonImage forState:UIControlStateNormal]; [self.view addSubview:button]; }
改变图片颜色代码
// // UIImage+Developer.m // Test // // Created by XuTianyu on 16/3/3. // Copyright © 2016年 lakala. All rights reserved. // #import "UIImage+Developer.h" @implementation UIImage (Developer) /** * 重新绘制图片 * * @param color 填充色 * * @return UIImage */ - (UIImage *)imageWithColor:(UIColor *)color { UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale); CGContextRef context = 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; } @end刚才设置button图片的话改成
[button setImage:[buttonImage imageWithColor:[UIColor blueColor]]forState:UIControlStateNormal];运行看效果