-1

Firstly, I am converting an image original to gray scale and its successfully converted. But the problem is, how to convert gray scale back to original image by user touch on that place. What I'm unable to understand is how to convert **Gray Scale to original **.

Here is my code ** Original to gray scale **

- (UIImage *)convertImageToGrayScale:(UIImage *)image
{
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
    CGContextDrawImage(context, imageRect, [image CGImage]);
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *newImage = [UIImage imageWithCGImage:imageRef];
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CFRelease(imageRef);
    return newImage;
}

Guidance needed. Thanks in advance.

Dhrumil
  • 3,221
  • 6
  • 21
  • 34

2 Answers2

1

You can't convert a gray scale image back to color because you no longer have any color information in the image data.

If you mean you have a color image that you're converting to gray scale, and then when the user taps you show a color version, then instead you need to hang on to the original image and show that one in color.

SomeGuy
  • 9,670
  • 3
  • 32
  • 35
  • NO i want after converting image on gray scale. then i want swipe finger on screen.on whose screen finger swipe . that area image converted on his original color. i have a example quick splash app. – user3899098 Aug 01 '14 at 11:21
  • 1
    @user3899098 it sounds like instead of asking how to convert from grayscale to rgb you want to know how to swap one grayscale image for a colored image when the user taps the screen, is this what you're asking? – SomeGuy Aug 01 '14 at 12:17
0

I am using fixes size images which are 98 by 98 pixels. What I ended up doing is created a blank 98 by 98 png in Photoshop and calling it rgboverlay.png. Then I just overlay my grayscale image on top of the blank one and the resulting image is RGB. Here's the code. I originally got the code to overlay one image on another.

originalimage = your grayscale image

static UIImage* temp = nil;
thumb = [UIImage imageNamed:@"rgboverlay.png"];
CGSize size = CGSizeMake(98, 98);
UIGraphicsBeginImageContext(size);
CGPoint tempPoint = CGPointMake(0, 25 - temp.size.height / 2);
[temp drawAtPoint:testPoint];
CGPoint starredPoint = CGPointMake(1, 1);
[originalimage drawAtPoint:starredPoint];
// result is the new RGB image
result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This ended up working for me.

  • 1
    if my answer is not working try this http://stackoverflow.com/questions/2619668/how-to-convert-a-grayscale-matrix-to-an-rgb-matrix-in-matlab –  Aug 01 '14 at 11:29