Ios 如何在不改变颜色空间的情况下获得图像负颜色

Ios 如何在不改变颜色空间的情况下获得图像负颜色,ios,objective-c,uiimage,cgcolorspace,Ios,Objective C,Uiimage,Cgcolorspace,所以,我遵循了这个问题的建议: 但是当我进行转换时,颜色空间信息丢失并恢复为RGB。(我想要灰色的) 如果在给定代码之前和之后输入NSLogCGColorSpaceRef,则会确认这一点 CGColorSpaceRef before = CGImageGetColorSpace([imageView.image CGImage]); NSLog(@"%@", before); UIGraphicsBeginImageContextWithOptions(imageView.image.siz

所以,我遵循了这个问题的建议:

但是当我进行转换时,颜色空间信息丢失并恢复为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格式存储位图数据。如果“不透明”参数为“是”,则位图将被视为完全不透明,其alpha通道将被忽略


因此,如果不将其更改为
CGContext
,这可能是不可能的?我发现,如果我将
不透明
参数设置为
,则它会删除alpha通道,这是足够的(我使用的tiff读取器无法处理ARGB图像)。我仍然希望只使用灰度图像,以减小文件大小。

我发现解决此问题的唯一方法是添加另一种方法,在反转图像后将其重新转换为灰度。我添加了这个方法:

- (UIImage *)convertImageToGrayScale:(UIImage *)image
{
// Create image rectangle with current image width/height
CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);

// Grayscale color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

// Create bitmap content with current image size and grayscale colorspace
CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);

// Draw image into current context, with specified rectangle
// using previously defined context (with grayscale colorspace)
CGContextDrawImage(context, imageRect, [image CGImage]);

// Create bitmap image info from pixel data in current context
CGImageRef imageRef = CGBitmapContextCreateImage(context);

// Create a new UIImage object
UIImage *newImage = [UIImage imageWithCGImage:imageRef];

// Release colorspace, context and bitmap information
CGColorSpaceRelease(colorSpace);
CGContextRelease(context);
CFRelease(imageRef);

// Return the new grayscale image
return newImage;
}
如果有人有更整洁的方法,我很乐意听到