掩盖图像并填充颜色

时间:2013-05-20 09:54:32

标签: ios mask

我想用一种颜色掩盖image并填充选择和其余部分。 我正在使用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];

感谢您的帮助 编辑:我想我不清楚,我想选择一种颜色和其他颜色

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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;
    }

答案 2 :(得分:0)

您收到的问题为零,因为您发送的参数无效。如果您打开Apple文档,您将看到:

<强>零件

  

指定颜色或颜色范围的颜色组件数组   使用屏蔽图像。该数组必须包含2N个值{min 1,   max 1,... min [N],max [N]}其中N是组件的数量   图像的色彩空间。组件中的每个值都必须是有效图像   样本价值。如果图像具有整数像素分量,那么每个值   必须在[0 .. 2 ** bitsPerComponent - 1]范围内(其中   bitsPerComponent是图像的位数/分量数。如果图像   具有浮点像素分量,则每个值可以是任意值   浮点数,它是一个有效的颜色分量。

尝试不同的参数,例如

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

I also found a related Question on SO.

相关问题