例如,在iOS工具栏中,您可以拖入您自己的.opaque黑色和透明的.png文件,它会自动添加iOS蓝色的光泽效果。
然而,我想知道如何自己实现这个效果,只用纯色就行了。
例如,如果您有这张图片:
您要怎么做才能使整张图片变成纯粉色、蓝色或灰色呢?我想通过编码来减少我在应用程序中保存版本的数量。
谢谢!
然而,我想知道如何自己实现这个效果,只用纯色就行了。
例如,如果您有这张图片:
![Example](https://p.twimg.com/A0jLYKxCEAEqUZB.jpg)
谢谢!
这应该是适合你的:
- (UIImage *)colorImage:(UIImage *)origImage withColor:(UIColor *)color
{
UIGraphicsBeginImageContextWithOptions(origImage.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, (CGRect){ {0,0}, origImage.size} );
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, origImage.size.height);
CGContextConcatCTM(context, flipVertical);
CGContextDrawImage(context, (CGRect){ pt, origImage.size }, [origImage CGImage]);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
UIImage *image = [UIImage imageNamed:@"myImage.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]];
imageView.tintColor = [UIColor redColor];