2013-05-20 153 views
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]; 

感谢您的帮助 编辑:我想我是不太清楚,我想对于选择一种颜色和另一种颜色,其余

+0

试这些参数'const float myMaskingColors [6] = {1.0,1.0,0.0,0.0,1.0,1.0}; ' – Buntylm

回答

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

在这种情况下,我不能有2种颜色。我编辑了我的帖子。 – user2401221

0

您的艾米问题得到零,因为您发送的参数是无效的。如果你打开苹果的文件,你会看到:

组件

指定一种颜色或颜色 范围,以掩盖与图像色彩分量的数组。该阵列必须包含2N个值{最小1, 最大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.

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; 
} 
+0

我将渐变应用于(UIImageView *)imageView的.image,但似乎没有工作。 –