[英]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.我正在使用 98 x 98 像素的修复尺寸图像。 What I ended up doing is created a blank 98 by 98 png in Photoshop and calling it rgboverlay.png.我最终做的是在 Photoshop 中创建了一个 98 x 98 的空白 png,并将其命名为 rgboverlay.png。 Then I just overlay my grayscale image on top of the blank one and the resulting image is RGB.然后我只是将我的灰度图像叠加在空白图像的顶部,结果图像是 RGB。 Here's the code.这是代码。 I originally got the code to overlay one image on another.我最初得到了将一个图像叠加在另一个图像上的代码。
originalimage = your grayscale image originalimage = 您的灰度图像
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.这最终对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.