如何使用 CGImageCreateWithMaskingColors 应用多个颜色蒙版?

发布于 2024-10-16 15:31:13 字数 1154 浏览 7 评论 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;
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野侃 2024-10-23 15:31:13

好吧,我找到了一个很好的解决办法!基本上,我最终是通过 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

希望这可以帮助别人!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文