gpt4 book ai didi

ios - 色调正在快速反转颜色

转载 作者:行者123 更新时间:2023-11-29 01:38:14 26 4
gpt4 key购买 nike

我正在尝试使用此代码为 UIImage 着色,但它似乎在反转颜色 - 为背景着色并将黑色描边变为白色。

我如何让它为图像的黑色描边线着色,而保留白色背景?这是一个 playground 示例,我在其中尝试了一种技术以及第一个答案建议的另一种技术。

Playground 代码

 extension UIImage {

func tint(color: UIColor, blendMode: CGBlendMode = CGBlendMode.Normal) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
let context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, blendMode);
let rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
color.setFill()
CGContextFillRect(context, rect);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

func tint2(color: UIColor, blendMode: CGBlendMode) -> UIImage
{
let drawRect = CGRectMake(0.0, 0.0, size.width, size.height)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()
CGContextClipToMask(context, drawRect, CGImage)
color.setFill()
UIRectFill(drawRect)
drawInRect(drawRect, blendMode: blendMode, alpha: 1.0)
let tintedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return tintedImage
}

}

let stringUrl = "https://s3.amazonaws.com/neighbor-chat-assets/icons/avatar1000.png"

let url = NSURL(string: stringUrl)

let data = NSData(contentsOfURL: url!)

var originalimage = UIImage(data: data!)

var image = originalimage!.imageWithRenderingMode(.AlwaysTemplate).tint(UIColor.blueColor(), blendMode:.Normal)
var image2 = originalimage!.imageWithRenderingMode(.AlwaysTemplate).tint2(UIColor.blueColor(), blendMode:.Normal)

最佳答案

这应该可以,我只是用 png 和透明背景测试过它

func tint(color: UIColor, blendMode: CGBlendMode = CGBlendMode.Normal) -> UIImage
{
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
let context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, blendMode);
let rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextClipToMask(context, rect, self.CGImage);
color.setFill()
CGContextFillRect(context, rect);
let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

另见 this post了解更多信息。另一种方法是将图像包装在具有 UIImageRenderingMode.AlwaysTemplate 呈现模式的 UIImageView 中,然后设置 tintColor 属性。

编辑

如果图像中没有 alpha channel ,那么这是您可以用来首先屏蔽掉某些颜色的函数

func tint(color: UIColor, blendMode: CGBlendMode = CGBlendMode.Normal, colorToMask: UIColor = UIColor.blackColor()) -> UIImage
{
var fRed : CGFloat = 0
var fGreen : CGFloat = 0
var fBlue : CGFloat = 0
var fAlpha: CGFloat = 0
colorToMask.getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha)

let rect = CGRectMake(0, 0, self.size.width, self.size.height);
var colorMasking : [CGFloat] = [fRed,fRed,fGreen,fGreen,fBlue,fBlue]
let newImageRef = CGImageCreateWithMaskingColors(self.CGImage!, &colorMasking)

UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale);
let context = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(context, 0, self.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetBlendMode(context, blendMode);
CGContextClipToMask(context, rect, newImageRef!);
color.setFill()
CGContextFillRect(context, rect);

let newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return newImage;
}

这个函数首先根据指定的颜色创建一个 mask ,并将上下文裁剪到这个 mask 上。

编辑 #2

如评论中所述,我也没有设法屏蔽掉白色,我尝试了 UInt8 值(即 255)和浮点值(即 1.0),但仍然无效。所以我能想到的唯一解决办法就是反转图像。这是函数:

func negativeImage() -> UIImage {

let coreImage = MYImage(CGImage: self.CGImage!)
let filter = CIFilter(name: "CIColorInvert", withInputParameters: ["inputImage" : coreImage])!
let result = filter.outputImage!
let context = CIContext(options:nil)
let cgimg : CGImageRef = context.createCGImage(result, fromRect: result.extent)

let bmpcontext = CGBitmapContextCreate(nil, Int(self.size.width), Int(self.size.height), 8, Int(self.size.width)*4, CGColorSpaceCreateDeviceRGB(),CGImageAlphaInfo.NoneSkipLast.rawValue)
CGContextDrawImage(bmpcontext, CGRectMake(0, 0, self.size.width, self.size.height), cgimg)
let newImgRef = CGBitmapContextCreateImage(bmpcontext)!
return UIImage(CGImage: newImgRef)
}

你可以这样使用它

var image : UIImage = ...
image = image.negativeImage()
image = image.tint(UIColor.redColor())

一般注意事项

  • CGImageCreateWithMaskingColors 需要带有 NO alpha channel 的 CGImage,这就是为什么我在 negativeImage 函数中使用标志 CGImageAlphaInfo.NoneSkipLast 以移除又是 alpha channel
  • 这里绝对没有错误检查,您至少应该使用 CGImageGetAlphaInfo
  • 检查 tint 函数中是否包含 alpha channel
  • 您可以使被遮盖的部分(在我们的示例中为白色)透明(通过在 tint 函数中的 UIGraphicsBeginImageContextWithOptions 命令中设置 false),或者通过设置 true 来设置特定颜色,即黑色默认情况下(您需要在剪裁到蒙版之前进行绘画)。

关于ios - 色调正在快速反转颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32727807/

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