简体   繁体   English

在 UIImage 中 iOS 颜色为透明

[英]iOS color to transparent in UIImage

How can I clear out the magenta part of an UIImage and make it transparent?如何清除 UIImage 的洋红色部分并使其透明?

I've looked through numerous answers and links on SO and nothing works (eg How to make one color transparent on a UIImage? answer 1 removes everything but red, answer 2 apparently doesn't work because of Why is CGImageCreateWithMaskingColors() returning nil in this case? ).我已经查看了许多关于 SO 的答案和链接,但没有任何效果(例如, 如何在 UIImage 上使一种颜色透明?答案 1 删除了除红色之外的所有内容,答案 2 显然不起作用,因为为什么 CGImageCreateWithMaskingColors() 在这种情况? )。

Update:更新:

If I use CGImageCreateWithMaskingColors with the UIImage I get a nil value.如果我将 CGImageCreateWithMaskingColors 与 UIImage 一起使用,我会得到一个 nil 值。 If I remove the alpha channel (I represent the image as JPEG and read it back) CGImageCreateWithMaskingColors returns an image painted with a black background.如果我删除 alpha 通道(我将图像表示为 JPEG 并将其读回) CGImageCreateWithMaskingColors 返回一个用黑色背景绘制的图像。

Update2, the code: Update2,代码:

Returning nil:返回零:

const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(anchorWithMask.CGImage, colorMasking);

NSLog(@"image ref %@", imageRef);
// this is going to return a nil imgref.

UIImage *image = [UIImage imageWithCGImage:imageRef];

Returning an image with black background (which is normal since there is not alpha channel):返回黑色背景的图像(这是正常的,因为没有 alpha 通道):

UIImage *inputImage = [UIImage imageWithData:UIImageJPEGRepresentation(anchorWithMask, 1.0)];

const float colorMasking[6] = {222, 255, 222, 255, 222, 255};
CGImageRef imageRef = CGImageCreateWithMaskingColors(inputImage.CGImage, colorMasking);

NSLog(@"image ref %@", imageRef);
// imgref is NOT nil.

UIImage *image = [UIImage imageWithCGImage:imageRef];

Update3:更新3:

I got it working by adding the alpha channel after the masking process.我通过在屏蔽过程之后添加 alpha 通道来让它工作。

I made this static function that removes the white background you can use it replacing the mask with the color range you want to remove:我制作了这个去除白色背景的静态函数,您可以使用它用要删除的颜色范围替换蒙版:

+ (UIImage*) processImage :(UIImage*) image
{
    const float colorMasking[6]={222,255,222,255,222,255};
    CGImageRef imageRef = CGImageCreateWithMaskingColors(image.CGImage, colorMasking);
    UIImage* imageB = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return imageB;
}
UIImage *image = [UIImage imageNamed:@"image.png"];

const float colorMasking[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};
image = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(image.CGImage, colorMasking)];

You receive nil, because parameters, that you send is invalid.您收到 nil,因为您发送的参数无效。 If you open Apple documentation, you will see this:如果您打开 Apple 文档,您将看到:

Components成分
An array of color components that specify a color or range of colors to mask the image with.一组颜色分量,用于指定用于遮罩图像的颜色或颜色范围。 The array must contain 2N values { min[1], max[1], ... min[N], max[N] } where N is the number of components in color space of image.该数组必须包含 2N 个值 { min[1], max[1], ... min[N], max[N] } 其中 N 是图像颜色空间中的分量数。 Each value in components must be a valid image sample value.组件中的每个值都必须是有效的图像样本值。 If image has integer pixel components, then each value must be in the range [0 .. 2**bitsPerComponent - 1] (where bitsPerComponent is the number of bits/component of image).如果图像具有整数像素分量,则每个值必须在 [0 .. 2**bitsPerComponent - 1] 范围内(其中 bitsPerComponent 是图像的位数/分量)。 If image has floating-point pixel components, then each value may be any floating-point number which is a valid color component.如果图像具有浮点像素分量,则每个值可以是作为有效颜色分量的任何浮点数。

You can quickly open documentation by holding Option + Mouse Click on some function or class, like CGImageCreateWithMaskingColors.您可以通过按住 Option + 鼠标单击某些函数或类来快速打开文档,例如 CGImageCreateWithMaskingColors。

I did this in CIImage with the post-processing from Vision being a pixelBuffer:我在 CIImage 中做了这个,来自 Vision 的后处理是一个 pixelBuffer:

    let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
    let filteredImage = ciImage.applyingFilter("CIMaskToAlpha")
    self.picture.image = UIImage(ciImage: filteredImage)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM