开发者

How do I apply multiple color masks using CGImageCreateWithMaskingColors?

I'm a bit new to objective-c and even newer at programming with Quartz 2D, so apologies in advance! I've got a method where I would like to remove a handful of specific colors (not just one) from a UIImage.

When I run my project with just one color mask being applied, it works beautifully. Once I try stacking them, the 'whiteRef' comes out NULL. I've even tried modifying my method to take a color mask and then simply ran my method twice -feeding in the different colors masks- but still no go.

Any help with this is greatly appreciated!

- (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] initWith开发者_开发百科Image: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;
}


Well I found a good work around! Basically, I was ultimately using the image data over a MKMapView, so all I needed to do was break down the image to it's pixels and from there I could mess around as a I please. It may not be the best option in terms of speed, but does the trick. Here is a sample of the code I'm now using.

//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;
}

I also borrowed some methods (ie: convertUIImageToBitmapRGBA8) from here: https://gist.github.com/739132

Hope this helps someone out!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜