你可以试试:
_sketchImageView.image = [self imageNamed:@"imageName" withColor:[UIColor blackColor]];
- (UIImage *)imageNamed:(NSString *)name withColor:(UIColor *)color
{
// load the image
//NSString *name = @"badge.png";
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;
}
如果你想填补图像与一些颜色,你需要更先进的解决方案。像这样:https://github.com/mxcl/UIImageWithColor – orkenstein 2015-02-10 09:19:56
我认为你需要背景颜色的效果,试试'[_sketchImageView setBackgroundColor:color]' – Saif 2015-02-10 09:22:20
我以前试过这个解决方案,它给出了相同的结果。 // UIImage * img = [UIImage resizableImageWithColor:color cornerRadius:10]; // UIImage * img = [UIImage imageWithColor:color size:CGSizeMake(20,20)]; UIImage * img = [UIImage imageWithColor:color]; _sketchImageView.image = img; – user2168496 2015-02-10 09:36:34