对图像进行遮罩并填充颜色

3

我想用一种颜色遮盖并填充选定的部分和其余部分中的图像。

我正在使用CGImageCreateWithMaskingColors,但是我不知道如何用另一种颜色填充image

以下是我的代码开头:

UIImage *source = [UIImage imageNamed:@"nb.png"];

const CGFloat myMaskingColors[6] = {0,110,0,110,0,110};
CGImageRef imageRef = CGImageCreateWithMaskingColors(source.CGImage, myMaskingColors);
UIImage* imageB = [UIImage imageWithCGImage:imageRef];
UIImageView *imageView = [[UIImageView alloc]initWithImage:imageB];

谢谢你的帮助 编辑:我觉得我没有表达清楚,我想要一个颜色用于选定部分,另一个颜色用于其余部分。


请尝试使用以下参数:const float myMaskingColors[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0}; - Buntylm
3个回答

1
如果您想在图像上应用2种颜色,则可以像这样在图像上应用渐变。
- (UIImage *)applyGradientOnImage:(UIImage *)image withStartColor:(UIColor *)color1 endColor:(UIColor *)color2 {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, 0, image.size.height);
    CGContextScaleCTM(context, 1.0, -1.0);

    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    //CGContextDrawImage(context, rect, img.CGImage);

    // Create gradient
    NSArray *colors = [NSArray arrayWithObjects:(id)color2.CGColor, (id)color1.CGColor, nil];
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);

    // Apply gradient
    CGContextClipToMask(context, rect, image.CGImage);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, image.size.height), 0);
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGGradientRelease(gradient);
    CGColorSpaceRelease(space);

    return gradientImage;
}

我将渐变应用于(UIImageView *)imageView的.image,但似乎没有起作用。 - Fang Chen

0

您可以通过应用单一颜色的渐变来实现这一点。下面的代码执行了我们需要的相同操作

- (UIImage *)applyColor:(UIColor *)color toImage:(UIImage*)toImage{
        UIGraphicsBeginImageContextWithOptions(toImage.size, NO, toImage.scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, toImage.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);

        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGRect rect = CGRectMake(0, 0, toImage.size.width, toImage.size.height);
        //CGContextDrawImage(context, rect, img.CGImage);

        // Create gradient
        NSArray *colors = [NSArray arrayWithObjects:(id)color.CGColor, (id)color.CGColor, nil];
        CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
        CGGradientRef gradient = CGGradientCreateWithColors(space, (__bridge CFArrayRef)colors, NULL);

        // Apply gradient
        CGContextClipToMask(context, rect, toImage.CGImage);
        CGContextDrawLinearGradient(context, gradient, CGPointMake(0,0), CGPointMake(0, toImage.size.height), 0);
        UIImage *coloredImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CGGradientRelease(gradient);
        CGColorSpaceRelease(space);

        return coloredImage;
    }

0

你可能会收到nil的问题,因为你发送的参数是无效的。如果你查看苹果文档,你会看到以下内容:

Components

一个颜色组件数组,用于指定要掩盖图像的颜色或颜色范围。该数组必须包含2N个值{min1, max1, ...min[N],max[N]},其中N是图像颜色空间中的组件数。components中的每个值都必须是有效的图像样本值。如果图像具有整数像素组件,则每个值必须在范围[0 .. 2**bitsPerComponent - 1]内(其中bitsPerComponent是图像的位数/组件)。如果图像具有浮点像素组件,则每个值可以是任何有效的颜色组件浮点数。

尝试不同的参数,如

const float myMaskingColors[6] = {1.0, 1.0, 0.0, 0.0, 1.0, 1.0};

我在 Stack Overflow 上也找到了一个相关的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接