Xcode 如何使用CGImageCreateWithMaskingColor应用多个颜色遮罩?

Xcode 如何使用CGImageCreateWithMaskingColor应用多个颜色遮罩?,xcode,quartz-2d,masking,Xcode,Quartz 2d,Masking,我对objective-c有点陌生,在使用Quartz 2D编程方面更是新手,所以请提前道歉!我有一种方法,我想从UIImage中删除一些特定的颜色(不仅仅是一种) 当我运行我的项目时,只应用了一个颜色遮罩,它工作得很好。一旦我尝试堆叠它们,“whiteRef”就会显示为空。我甚至尝试过修改我的方法,以获得一个彩色蒙版,然后简单地运行我的方法两次——输入不同颜色的蒙版——但仍然没有成功 非常感谢您的帮助 - (UIImage *)doctorTheImage:(UIImage *)origina

我对objective-c有点陌生,在使用Quartz 2D编程方面更是新手,所以请提前道歉!我有一种方法,我想从UIImage中删除一些特定的颜色(不仅仅是一种)

当我运行我的项目时,只应用了一个颜色遮罩,它工作得很好。一旦我尝试堆叠它们,“whiteRef”就会显示为空。我甚至尝试过修改我的方法,以获得一个彩色蒙版,然后简单地运行我的方法两次——输入不同颜色的蒙版——但仍然没有成功

非常感谢您的帮助

- (UIImage *)doctorTheImage:(UIImage *)originalImage
{
    const float brownsMask[6] = {124, 255, 68, 222, 0, 165};
    const float whiteMask[6] = {255, 255,  255, 255, 255, 255};

    UIImageView *imageView = [[UIImageView alloc] initWithImage:originalImage];

    UIGraphicsBeginImageContext(originalImage.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGImageRef brownRef = CGImageCreateWithMaskingColors(imageView.image.CGImage, brownsMask);    
    CGImageRef whiteRef = CGImageCreateWithMaskingColors(brownRef, whiteMask);    
    CGContextDrawImage (context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), whiteRef);

    CGImageRelease(brownRef);
    CGImageRelease(whiteRef);

    UIImage *doctoredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [imageView release];

    return doctoredImage;
}

我找到了一份不错的工作!基本上,我最终是在MKMapView上使用图像数据,所以我所需要做的就是将图像分解为像素,然后我可以随心所欲地到处乱搞。就速度而言,这可能不是最好的选择,但确实有效。下面是我现在使用的代码示例

//Split the images into pixels to mess with the image pixel colors individually
size_t bufferLength = gridWidth * gridHeight * 4;
unsigned char *rawData = nil;
rawData = (unsigned char *)[self convertUIImageToBitmapRGBA8:myUIImage];

grid = malloc(sizeof(float)*(bufferLength/4));

NSString *hexColor = nil;

for (int i = 0 ; i < (bufferLength); i=i+4)
{
hexColor = [NSString stringWithFormat: @"%02x%02x%02x", (int)(rawData[i + 0]),(int)(rawData[i + 1]),(int)(rawData[i + 2])];


//mess with colors how you see fit - I just detected certain colors and slapped 
//that into an array of floats which I later put over my mapview much like the 
//hazardmap example from apple.

if ([hexColor isEqualToString:@"ff0299"]) //pink
    value = (float)11;
if ([hexColor isEqualToString:@"9933cc"]) //purple
    value = (float)12;

//etc...

grid[i/4] = value;
}
//将图像拆分为像素以分别与图像像素颜色混淆
size\u t bufferLength=gridWidth*gridHeight*4;
无符号字符*rawData=nil;
rawData=(无符号字符*)[self-convertUIImageToBitmapRGBA8:myUIImage];
网格=malloc(sizeof(float)*(bufferLength/4));
NSString*hexColor=nil;
对于(int i=0;i<(bufferLength);i=i+4)
{
hexColor=[NSString stringWithFormat:@“%02x%02x%02x”,(int)(原始数据[i+0]),(int)(原始数据[i+1]),(int)(原始数据[i+2]);
//把你认为合适的颜色弄得一团糟-我只是发现了某些颜色并打了一巴掌
//将其转换为一个浮动数组,稍后我将其放置在我的mapview上,与
//来自苹果的hazardmap示例。
if([hexColor IsequalString:@“ff0299”])//粉红色
值=(浮动)11;
如果([hexColor IsequalString:@“9933cc”])//紫色
值=(浮动)12;
//等等。。。
网格[i/4]=数值;
}
我还从这里借用了一些方法(例如:convertUIImageToBitmapRGBA8):

希望这能帮到别人