2011-07-13 36 views
4

我有一個灰度級寶石頂視圖。iOS:將RGB濾鏡應用於灰度PNG

(PNG格式,所以具有α-組分)

我想創建12點小大小的按鈕,每一個不同的顏色,從該圖像。

爲了整潔,我想在代碼中做這個,而不是在一些藝術包中的外部。

任何人都可以提供一個方法(或甚至一些代碼)做到這一點?

PS我知道如何使用的代碼可笑量做到這一點在GL,我希望有使用核芯顯卡/核心動畫

編輯一個簡單的方法:工作方案,從拜迷死下面回答

CGSize targetSize = (CGSize){100,100}; 
    UIImage* image; 
    { 
     CGRect rect = (CGRect){ .size = targetSize }; 

     UIGraphicsBeginImageContext(targetSize); 
     { 
      CGContextRef X = UIGraphicsGetCurrentContext(); 

      UIImage* uiGem = [UIImage imageNamed: @"GemTop_Dull.png"]; 

      // draw gem 
      [uiGem drawInRect: rect]; 

      // overlay a red rectangle 
      CGContextSetBlendMode(X, kCGBlendModeColor) ; 
      CGContextSetRGBFillColor (X, 0.9, 0, 0, 1); 
      CGContextFillRect (X, rect); 

      // redraw gem 
      [uiGem drawInRect: rect 
        blendMode: kCGBlendModeDestinationIn 
         alpha: 1. ]; 

      image = UIGraphicsGetImageFromCurrentImageContext(); 
     } 
     UIGraphicsEndImageContext(); 
    } 
+0

我對看似多餘{}您的代碼示例非常好奇 - 什麼做這些做 – Rhubarb

+0

他們使代碼更清晰/封裝變量 –

+0

如果你想支持視網膜圖像,請確保您使用'UIGraphicsBeginImageContextWithOptions(targetSize,NO,0.0);'而不是'UIGraphicsBeginImageContext(targetSize);'。參數「NO」和「0.0」分別表示使用不透明上下文(YES或NO值)和縮放因子(0.0表示設備縮放因子,任何其他值用於顯式縮放因子)。 –

回答

4

做最簡單的方法是將圖像繪製成RGB-colorspaced CGBitmapContext,使用CGContextSetBlendMode設置kCGBlendModeColor,然後畫在它與純色(例如用CGContextFillRect)。

+0

我認爲這僅僅是去給這裏提到的問題:http://forums.macrumors.com/showthread.php?t=547339 –

+1

如果'CGContextFillRect'抹殺alpha通道,嘗試在上面繪製的原始圖像'kCGBlendModeDestinationIn'重新應用它。 – Anomie

+0

我只是試過(我修改了我的問題)......仍然無法擺脫它們的紅色背景。有任何想法嗎? –

1

最好看的結果將來自使用灰色值來索引從最暗的顏色到最亮的顏色的漸變。不幸的是,我不知道用核心圖形做這件事的細節。

+0

您可以使用[CIColorMap(https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CoreImageFilterReference/Reference/reference.html#//apple_ref/doc/uid/TP30000136-SW59)在覈心圖像包做這個。 – bcattle

+0

例如參見[這個問題](http://stackoverflow.com/questions/3182711/cicolormap-filter-error) – bcattle

0

這取決於該問題的答案的改善和@Anomie

的實現

首先,把這個在您的UIButton類,或者您的視圖控制器的開始。它將從UIColor轉換爲RGBA值,您稍後需要該值。

typedef enum { R, G, B, A } UIColorComponentIndices; 


@implementation UIColor (EPPZKit) 

- (CGFloat)redRGBAValue { 
    return CGColorGetComponents(self.CGColor)[R]; 
} 

- (CGFloat)greenRGBAValue { 
    return CGColorGetComponents(self.CGColor)[G]; 
} 

- (CGFloat)blueRGBAValue { 
    return CGColorGetComponents(self.CGColor)[B]; 
} 

- (CGFloat)alphaRGBAValue { 
    return CGColorGetComponents(self.CGColor)[A]; 
} 

@end 

現在,請確保您在IB中具有自定義圖像按鈕,帶有灰度圖像和正確的框架。這是再編程方式創建自定義圖像按鈕相當好,更容易,因爲:

  • 可以讓IB加載圖像,而不必手動加載它
  • 可以調節按鈕,看看它在視覺上IB
  • 您的IB看起來更像你的應用程序在運行時
  • 您不必手動設置幀

假設你有按鈕在IB(接近底部將支持對於有它編程方式創建),此方法添加到您的視圖控制器或按鈕崽類:

- (UIImage*)customImageColoringFromButton:(UIButton*)customImageButton fromColor:(UIColor*)color { 
    UIImage *customImage = [customImageButton.imageView.image copy]; 

    UIGraphicsBeginImageContext(customImageButton.imageView.frame.size); { 
     CGContextRef X = UIGraphicsGetCurrentContext(); 

     [customImage drawInRect: customImageButton.imageView.frame]; 

     // Overlay a colored rectangle 
     CGContextSetBlendMode(X, kCGBlendModeColor) ; 
     CGContextSetRGBFillColor (X, color.redRGBAValue, color.greenRGBAValue, color.blueRGBAValue, color.alphaRGBAValue); 
     CGContextFillRect (X, customImageButton.imageView.frame); 

     // Redraw 
     [customImage drawInRect:customImageButton.imageView.frame blendMode: kCGBlendModeDestinationIn alpha: 1.0]; 

     customImage = UIGraphicsGetImageFromCurrentImageContext(); 
    } 
    UIGraphicsEndImageContext(); 

    return customImage; 
} 

然後,您將需要調用它的設置方法,在您的視圖控制器或按鈕的子類,並設置的ImageView的該按鈕將其:

[myButton.imageView setImage:[self customImageColoringFromButton:myButton fromColor:desiredColor]]; 

如果您是使用IB創建按鈕,使用此方法:

- (UIImage*)customImageColoringFromImage:(UIImage*)image fromColor:(UIColor*)color fromFrame:(CGRect)frame { 
    UIImage *customImage = [image copy]; 

    UIGraphicsBeginImageContext(frame.size); { 
     CGContextRef X = UIGraphicsGetCurrentContext(); 

     [customImage drawInRect: frame]; 

     // Overlay a colored rectangle 
     CGContextSetBlendMode(X, kCGBlendModeColor) ; 
     CGContextSetRGBFillColor (X, color.redRGBAValue, color.greenRGBAValue, color.blueRGBAValue, color.alphaRGBAValue); 
     CGContextFillRect (X, frame); 

     // Redraw 
     [customImage drawInRect:frame blendMode: kCGBlendModeDestinationIn alpha: 1.0]; 

     customImage = UIGraphicsGetImageFromCurrentImageContext(); 
    } 
    UIGraphicsEndImageContext(); 

    return customImage; 
} 

而且與調用它:

[self.disclosureButton.imageView setImage:[self customImageColoringFromImage:[UIImage imageNamed:@"GemTop_Dull.png"] fromColor:desiredColor fromFrame:desiredFrame]];