gpt4 book ai didi

ios - iOS:将UIImage中的黑色更改为另一种颜色

转载 作者:行者123 更新时间:2023-12-01 17:22:35 24 4
gpt4 key购买 nike

我有一个白色边框和黑色填充的图像。我只想在运行时将黑色更改为另一种颜色。用户将在运行时以HSB格式选择颜色。我怎样才能做到这一点?我通过尝试尝试 CGImageCreateWithMaskingColors
const float colorMasking [4] = {255,255,255,255};
但我每次都会得到nil CGImageRef 。请帮忙。

- (UIImage*) maskBlackInImage :(UIImage*) image color:(UIColor*)color
{
const CGFloat colorMasking[4] = { 222, 255, 222, 255 };
CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
UIImage* imageB = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return imageB;

}

我要附加灯泡的图像-灯泡具有黑色填充,白色边框和透明背景

更新:

在接受的答案中使用代码后,我能够用另一种颜色填充黑色。但是,我可以在白色边框上看到一点点颜色。图像看起来不那么清晰。附加输出:

最佳答案

创建一个UIImage类的类别并添加以下方法

- (UIImage *)imageTintedWithColor:(UIColor *)color
{
UIImage *image;
if (color) {
// Construct new image the same size as this one.
UIGraphicsBeginImageContextWithOptions([self size], NO, 0.0); // 0.0 for scale means "scale for device's main screen".
CGRect rect = CGRectZero;
rect.size = [self size];

// tint the image
[self drawInRect:rect];
[color set];
UIRectFillUsingBlendMode(rect, kCGBlendModeScreen);

// restore alpha channel
[self drawInRect:rect blendMode:kCGBlendModeDestinationIn alpha:1.0f];

image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return image;
}

关于ios - iOS:将UIImage中的黑色更改为另一种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26115407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com