我有一组我创建的透明白色PNG图标:

而我想做的是能够将它们染成其他颜色.如蓝色,灰色等
我注意到"点击/点按"它们会自动变为灰色.所以我假设我可以通过点击或其正常状态将灰色改为我喜欢的任何颜色:

实现这一目标的最佳方法是什么?
我有一个叫做的图像arrowWhite.我想将此图像着色为黑色.
func attachDropDownArrow() -> NSMutableAttributedString {
let image:UIImage = UIImage(named: "arrowWhite.png")!
let attachment = NSTextAttachment()
attachment.image = image
attachment.bounds = CGRectMake(2.25, 2, attachment.image!.size.width - 2.25, attachment.image!.size.height - 2.25)
let attachmentString = NSAttributedString(attachment: attachment)
let myString = NSMutableAttributedString(string: NSString(format: "%@", self.privacyOptions[selectedPickerRow]) as String)
myString.appendAttributedString(attachmentString)
return myString
}
Run Code Online (Sandbox Code Playgroud)
我希望得到这张图片blackColour.
tintColor不管用...
我很想知道如何为图像着色(例如,制作白色.png红色).我已经看到了各种建议,但从未确认这实际上是可行的.我试过这个:
-(UIImage *)colorizeImage:(UIImage *)baseImage color:(UIColor *)theColor {
UIGraphicsBeginImageContext(baseImage.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSaveGState(ctx);
CGContextClipToMask(ctx, area, baseImage.CGImage);
[theColor set];
CGContextFillRect(ctx, area);
CGContextRestoreGState(ctx);
CGContextSetBlendMode(ctx, kCGBlendModeNormal);
CGContextDrawImage(ctx, area, baseImage.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
myImageView.image = [self colorizeImage:[UIImage imageNamed:@"whiteImage.png"] color:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
但它不起作用 - 屏幕上的图像仍然是白色的.
我正在研究Iphone应用程序.
我有png代表符号的图片.符号全部为黑色,背景透明.
有没有办法可以将黑色变成另一种颜色?我正在寻找的东西可以帮助我选择我想要的颜色和使用符号(在UIImage中)我可以使它们看起来像我选择的颜色.
我搜索过并发现了一个名为OpenCV的框架,它可以操作图像,但我无法找到如何重新着色图片.
非常感谢任何帮助和建议.
谢谢
我有一个部分透明的单色图像.我有图像的普通版和@ 2X版.我希望能够在代码中为图像着色不同的颜色.下面的代码适用于普通图像,但@ 2X最终会产生伪像.正常图像可能有类似问题如果是这样,我无法通过分辨率检测到它.
+(UIImage *) newImageFromMaskImage:(UIImage *)mask inColor:(UIColor *) color {
CGImageRef maskImage = mask.CGImage;
CGFloat width = mask.size.width;
CGFloat height = mask.size.height;
CGRect bounds = CGRectMake(0,0,width,height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);
CGContextClipToMask(bitmapContext, bounds, maskImage);
CGContextSetFillColorWithColor(bitmapContext, color.CGColor);
CGContextFillRect(bitmapContext, bounds);
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(bitmapContext);
CGContextRelease(bitmapContext);
UIImage *result = [UIImage imageWithCGImage:mainViewContentBitmapContext];
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果重要,则使用加载蒙版图像UIImage imageNamed:.此外,我确认在视网膜模拟器上运行时正在加载@ 2X图像.
更新:以上代码有效.我看到的工件是由图像消费者完成的其他变换引起的.这个问题可以删除,因为它不再是一个问题,或留给后人.