0

我对objective-c有点陌生,甚至对使用Quartz 2D编程也比较陌生,所以提前道歉!我有一种方法,我想从 UIImage 中删除一些特定颜色(不仅仅是一种)。

当我只应用一种颜色蒙版运行我的项目时,它的效果非常好。一旦我尝试堆叠它们,“whiteRef”就会出现 NULL。我什至尝试修改我的方法以获取颜色蒙版,然后简单地运行我的方法两次 - 输入不同颜色的蒙版 - 但仍然没有成功。

非常感谢您对此的任何帮助!

- (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;
}
4

1 回答 1

1

好吧,我找到了一个很好的工作!基本上,我最终是在 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;
}

我还从这里借用了一些方法(即:convertUIImageToBitmapRGBA8):https ://gist.github.com/739132

希望这可以帮助某人!

于 2011-02-19T02:47:14.720 回答