2011-07-13 14 views
4

にRGBフィルタを適用すると、私はグレースケール宝石のトップビューを持っています。のiOS:グレースケールPNG

私はこの画像から、異なる色で12個の小型ボタン、それぞれを作成したい

(PNG形式は、そうアルファ成分を有します)。

整頓のために、私はいくつかの芸術パッケージの外部コード内でこれを行うのではなくしたいと思います。

誰もがこれを行うための方法(あるいはいくつかのコード)を提供することができますか? 、ワーキング溶液から素晴らしさのおかげで私は、コードのとんでもない量を使用してGLでそれを行う方法を知っています

PSは、私はコアグラフィックス/コアアニメーション

EDITを使用して簡単な方法がある願っています答え

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);の代わりに 'UIGraphicsBeginImageContextWithOptions(targetSize、NO、0.0);を使います。パラメータ「NO」および「0.0」は、不透明なコンテキスト(YESまたはNO値)およびスケールファクタ(デバイススケールファクタの場合は0.0、明示的なスケールファクタの場合はその他の値)をそれぞれ用いて示される。 –

答えて

4

の下にそれを行うための最も簡単な方法は、kCGBlendModeColorを設定し、ソリッドカラー(例えばCGContextFillRect付き)とその上に描画するCGContextSetBlendModeを使用し、RGB-colorspaced CGBitmapContextに画像を描画することです。

+0

これはちょうどここに記載された問題を与えるつもりだと思う:http://forums.macrumors.com/showthread.php?t=547339 –

+1

'CGContextFillRect'がアルファチャンネルを抹消する場合は、元の画像を一番上に描画してみてくださいそれを再適用するために '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)をこれを行うCore Imageパッケージ。 – bcattle

+0

たとえば[この質問](http://stackoverflow.com/questions/3182711/cicolormap-filter-error)を参照してください。 – bcattle

0

これはあなたのUIButtonクラスの先頭、またはあなたのビューコントローラでこれを入れて、問題の解答時に改善し、@Anomie

まずの実装です。 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]]; 
    
  • 関連する問題