[英]iOS : How to convert an image gray scale to original by finger touch?
首先,我将原始图像转换为灰度并成功转换。 但问题是,如何通过用户触摸那个地方将灰度转换回原始图像。 我无法理解的是如何将**灰度转换为原始**。
这是我的代码**原始到灰度**
- (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;
}
需要指导。 提前致谢。
您无法将灰度图像转换回彩色,因为图像数据中不再有任何颜色信息。
如果您的意思是将彩色图像转换为灰度,然后当用户点击时显示彩色版本,那么您需要保留原始图像并以彩色显示该图像。
我正在使用 98 x 98 像素的修复尺寸图像。 我最终做的是在 Photoshop 中创建了一个 98 x 98 的空白 png,并将其命名为 rgboverlay.png。 然后我只是将我的灰度图像叠加在空白图像的顶部,结果图像是 RGB。 这是代码。 我最初得到了将一个图像叠加在另一个图像上的代码。
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();
这最终对我有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.