2012-06-05 17 views
10

在iOS 5.x中是否有簡單的方法(或內置庫)來使UIImage飽和?以下是我目前的工作方式:我如何去除UIImage的色差?

CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGContextTranslateCTM(context, 0.0, self.bounds.size.height); // flip image right side up 
    CGContextScaleCTM(context, 1.0, -1.0); 

    CGContextDrawImage(context, rect, self.image.CGImage); 
    CGContextSetBlendMode(context, kCGBlendModeSaturation); 
    CGContextClipToMask(context, self.bounds, image.CGImage); // restricts drawing to within alpha channel 
    CGContextSetRGBFillColor(context, 0.0, 0.0, 0.0, desaturation); 
    CGContextFillRect(context, rect); 

    CGContextRestoreGState(context); // restore state to reset blend mode 

這似乎比我預期的要複雜一些。有沒有比這更簡單的方法?

我正在考慮核心圖像,但我似乎無法找到一個具體的例子,如何使用它去飽和圖像。

+0

不確定,但可以嘗試應用[CIFilter](http://developer.apple.com/library/ios/#DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIFilter_Class/Reference/Reference.html) [kCICategoryColorEffect](http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/QuartzCoreFramework/Classes/CIFilter_Class/Reference/Reference.html#//apple_ref/doc/c_ref/kCICategoryColorEffect)類型。快速演示如何應用CIFilter:http://iazapps.wordpress.com/2012/02/01/applying-cifilter-on-image-in-an-uiimageview/ –

+0

什麼是你的去飽和值? – malhal

+0

indiekiduk,該代碼出現在更多上下文中的答案在這裏:http://stackoverflow.com/questions/1144768/how-can-i-change-the-saturation-of-an-uiimage(去飽和是在例)。 – JLundell

回答

21

可以使用兩個代碼或三行,我開源做到這一點GPUImage框架:

UIImage *inputImage = [UIImage imageNamed:@"inputimage.png"];  
GPUImageGrayscaleFilter *grayscaleFilter = [[GPUImageGrayscaleFilter alloc] init]; 
UIImage *grayscaleImage = [grayscaleFilter imageByFilteringImage:inputImage]; 

(記住釋放過濾器,如果使用ARC沒有建立)

這降低了圖像其亮度值將其去飽和。如果你想變飽和/去飽和,你可以使用GPUImageSaturationFilter。正如框架名稱所示,這種過濾在GPU上運行,並且幾乎在我從5.1開始在iOS上進行基準測試的每種情況下都比Core Image快。

+0

爲以後添加書籤!尼斯 – box86rowh

+0

布拉德是否有一種方法使飽和後的圖像與原來的色彩去飽和後?目前,當您使用GPUImage對圖像進行去飽和處理時,它看起來像是單向旅行。是否保留原始圖像的副本唯一的解決方案?我希望能有更像Photoshop的非破壞性飽和度調整圖層:P – Zhang

+0

@Zhang - 不,如果您想要保留原始的彩色圖像,因爲信息會在去飽和度中丟失。不過,您可以爲圖像和濾鏡設置多個目標,以便在執行去飽和之前分割第二個處理路徑。 Photoshop的無損濾鏡保留了原始圖像的副本。 –