tags:

views:

980

answers:

5

hi, how can i change the UIImage's Color through Programming,any help pls?if i send UIImage, its color must be changed ..any help please? if i change rgb color like the following through bitmaphandling it did not work.i have given blackcolor's RGB value like 0,0,0.....

A: 

The answer as a question: What if your UIImage contained a rainbow? What would you expect "changing the image's color" to do with such an image?

inked
FWIW - look up one of my answers on stackoverflow dealing with creating a bitmap image on the iPhone from data (I think Google has it as 'objective c make bitmap') What you're not saying is that your image is monotone. THAT is an important part of the question.
inked
we can manipulate pixel data,as we can get pixel color of UIImage,i am asking , can we change UIImage's RGB value through programming?
Mikhail Naimy
i have one image, i need the same image with different colors,so ifi add one image , if i change color of that image through programming,i can avoid adding lot of images with different colors
Mikhail Naimy
Depending on your image, it might be more efficient to have a lot of different images in different colors.
mahboudz
A: 

I think you can create another context with setting there context color to RGB you want to color your picture. Then draw your UIImage into that context and use that context instead of using directly your picture. This is a concept. This way you're creating offscreen buffer with a colored image. I didn't try this in cocoa, only in carbon, but i suppose it will work in the same way.

Nava Carmon
i did it as you said .....but it did not work , will you providethat code pls?
Mikhail Naimy
i get the context from that image using UIGraphicsBeginImageContext.but it did not work?
Mikhail Naimy
Please see this thread:http://stackoverflow.com/questions/1112947/how-to-create-a-uiimage-from-the-current-graphics-contextSo first set the current color in that context to the RGB you want to color your picture and then draw the picture. After you did it, use the context you've created, not the original picture
Nava Carmon
A: 

The RGB data you are operating on is just a copy. After you finish making changes, you need to turn that data back into an image.

I first make a new bitmap:

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
ctx = CGBitmapContextCreate( malloc(dataSize), width, height,
           8, // CGImageGetBitsPerComponent(cgImage),
           bytesPerRow, //CGImageGetBytesPerRow(cgImage),
           space,
           //kCGImageAlphaNoneSkipLast | kCGBitmapByteOrder32Big );
           kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little);
           //kCGImageAlphaPremultipliedFirst  | kCGBitmapByteOrder32Little);

    CGColorSpaceRelease( space );

// now draw the image into the context
CGRect rect = CGRectMake( 0, 0, CGImageGetWidth(cgImage), CGImageGetHeight(cgImage) );
CGContextDrawImage( ctx, rect, cgImage );

And get the pixels:

pixels = CGBitmapContextGetData( ctx );

Assuming that your pixel data came from pixels = CGBitmapContextGetData( ctx ); then take that context and build a new image from it:

CGImageRef newImg = CGBitmapContextCreateImage(ctx);
[[UIImage imageWithCGImage:newImg] drawInRect:rect];
CGImageRelease(newImg);
mahboudz
did it work for you? i tried , it did not give result please?
Mikhail Naimy
You're doing something wrong, and I can't tell you what it is without knowing what you do after you change the bitmap. How do you turn that back into an image?Search for "[iphone] bitmap image" here on StackOverflow. There are tons of questions and answers.
mahboudz
To make sure you are getting the pixel data correctly, read this http://developer.apple.com/mac/library/qa/qa2007/qa1509.html.
mahboudz
i did exactly what apple has told, see my question again please.....?returned image is not in black....bcos i have used r-0,g-0,b-0please
Mikhail Naimy
A: 

Hmmm -- isn't the order of the bytes supposed to be RGBA? You are setting them as ARGB...

jstevenco