所以,我跟着这个问题的建议:如何获得UIImage的负颜色不改变颜色空间
how to give UIImage negative color effect
但是当我做了转换,色彩空间信息丢失,并恢复到RGB。 (我想要灰色)。
如果我在NSLog
CGColorSpaceRef
之前和之后给出的代码,它证实了这一点。
CGColorSpaceRef before = CGImageGetColorSpace([imageView.image CGImage]);
NSLog(@"%@", before);
UIGraphicsBeginImageContextWithOptions(imageView.image.size, YES, imageView.image.scale);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
[imageView.image drawInRect:CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height)];
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height));
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
CGColorSpaceRef after = CGImageGetColorSpace([imageView.image CGImage]);
NSLog(@"%@", after);
是否有任何方法来保留颜色空间信息,或者,如果没有,我怎么能改变它之后呢?
编辑:在阅读文档UIGraphicsBeginImageContextWithOptions
它说:
对于iOS 3.2中创建位图和后来的绘图环境使用预乘ARGB格式存储的位图数据。如果opaque参数为YES,则位图将被视为完全不透明,并忽略其Alpha通道。
所以也许这是不可能的,没有改变它为CGContext
?我发现如果我将opaque
参数设置为YES
,那么它将删除足够的alpha通道(我正在使用的tiff阅读器无法处理ARGB图像)。尽管为了减小文件大小,我仍然只想要一个灰度图像。