views:

493

answers:

3

So I tried to use the Quartz CGImageCreateWithMaskingColors function, but he only problem is that it masks the color range you are selecting.

I want to mask everything but the color range I am selecting. For instance, I want to show all red colors in a picture but remove the other channels (Green and Blue).

I am doing this in Objective-C and I am a noob so please give me examples :)

Any help is greatly appreciated.

A: 

use these methods.i found them in one of the SO posts.

-(void)changeColor
{
UIImage *temp23=Image;//Pass your image here
CGImageRef ref1=[self createMask:temp23];
const float colorMasking[6] = {1.0, 3.0, 1.0, 2.0, 2.0, 3.0};
CGImageRef New=CGImageCreateWithMaskingColors(ref1, colorMasking);
UIImage *resultedimage=[UIImage imageWithCGImage:New];
EditImageView.image = resultedimage;
[EditImageView setNeedsDisplay];
}

-(CGImageRef)createMask:(UIImage*)temp
{
CGImageRef ref=temp.CGImage;
int mWidth=CGImageGetWidth(ref);
int mHeight=CGImageGetHeight(ref);
int count=mWidth*mHeight*4;
void *bufferdata=malloc(count);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGContextRef cgctx = CGBitmapContextCreate (bufferdata,mWidth,mHeight, 8,mWidth*4, colorSpaceRef, kCGImageAlphaPremultipliedFirst); 

CGRect rect = {0,0,mWidth,mHeight};
CGContextDrawImage(cgctx, rect, ref); 
bufferdata = CGBitmapContextGetData (cgctx);

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bufferdata, mWidth*mHeight*4, NULL);
CGImageRef savedimageref = CGImageCreate(mWidth,mHeight, 8, 32, mWidth*4, colorSpaceRef, bitmapInfo,provider , NULL, NO, renderingIntent);
CFRelease(colorSpaceRef);
return savedimageref;
}

then call changecolor method on a buttons click event and see the result

Rahul Vyas
A: 

Hi Rahul,

First let me appreciate for your good work. It would be a better way when you show me where to change the color. I don't know how i'm getting the image to be displayed in purple color.

It would be great, if you help me out...

Thanks in Advance,

arun
A: 

Hi guys, I found the answer for my above problem, Follow the above code of Rahul with some changes to set your own color,

-(void)changeColor { UIImage *temp23=Image;//Pass your image here CGImageRef ref1=[self createMask:temp23]; const float colorMasking[6] = {1.0, 3.0, 1.0, 2.0, 2.0, 3.0}; CGImageRef New=CGImageCreateWithMaskingColors(ref1, colorMasking); UIImage *resultedimage=[UIImage imageWithCGImage:New]; EditImageView.image = resultedimage; [EditImageView setNeedsDisplay]; }

-(CGImageRef)createMask:(UIImage*)temp { CGImageRef ref=temp.CGImage; int mWidth=CGImageGetWidth(ref); int mHeight=CGImageGetHeight(ref); int count=mWidth*mHeight*4; void *bufferdata=malloc(count);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGContextRef cgctx = CGBitmapContextCreate (bufferdata,mWidth,mHeight, 8,mWidth*4, 
                                            colorSpaceRef, kCGImageAlphaPremultipliedLast); 
CGRect rect = {0,0,mWidth,mHeight}; 
CGContextDrawImage(cgctx, rect, ref); 
CGContextSaveGState(cgctx); 
CGContextSetBlendMode(cgctx, kCGBlendModeColor); 
CGContextSetRGBFillColor (cgctx, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(cgctx, rect);


bufferdata = CGBitmapContextGetData (cgctx);
const float colorMasking[6] = {1.0, 3.0, 1.0, 2.0, 2.0, 3.0};
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, bufferdata, mWidth*mHeight*4, NULL);
CGImageRef savedimageref = CGImageCreate(mWidth,mHeight, 8, 32, mWidth*4, colorSpaceRef, bitmapInfo,provider , NULL, NO, renderingIntent);
CFRelease(colorSpaceRef);
return savedimageref;

}

arun