3

我想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];

感谢您的帮助编辑:我想我不清楚,我想要一种颜色用于选择,另一种颜色用于其余部分

4

3 回答 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;
}
于 2013-05-21T10:20:04.390 回答
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;
    }
于 2013-05-20T10:10:31.597 回答
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};

我还发现了一个关于 SO 的相关问题。

于 2013-05-20T10:20:40.567 回答