简体   繁体   中英

iOS : How to convert an image gray scale to original by finger touch?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM